# python期末 **Repository Path**: COCOAlice/final-term-of-python ## Basic Information - **Project Name**: python期末 - **Description**: No description available - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-01-21 - **Last Updated**: 2021-04-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 一、项目介绍 _**[云端部署pythonanywhere链接](http://alicecoco777.pythonanywhere.com/)**_ ### 1. 项目名称 带你走(也名:跟我走、follow me) ### 2. 平台简介 - 你是否有去到一个新城市想找一家小店到了附近怎么找都找不到的经历? - 你是否有开学来到新学校连去上课都会迷路的经历? - 你是否有觉得那个地点近在眼前却怎么也绕不过去的经历? 为了解决这类问题,本平台致力于专一精准的步行导航服务,帮助用户精准找到最方便快捷省时省力的短途路径引导。 ### 3. 加值宣言 本产品使用了 [高德API开放平台](https://lbs.amap.com/)提供的 **[地理编码API](https://lbs.amap.com/api/webservice/guide/api/georegeo)** 和 **[路径规划API](https://lbs.amap.com/api/webservice/guide/api/direction#walk)** ,并且利用python语言、flask web和HTML搭建网站框架与高德API调用融合。 ### 4. 页面流程图  # 二、问题描述 ### 1. 价值主张画布  ### 2.用户画像    # 三、解决方案 ### 1. 项目的整体规划 - 因为自己有一个项目小组是做地摊导航的,想尝试一下其中的一部分功能也就是近距离的导航,课上许智超老师也有说到过步行导航的可行性和需要性,比如在学校里近距离的导航是有些欠缺的,然后就想做这个小项目。本学期之前的课程上就有测试过这些高德api,于是就去找到了之前的测试代码,因为时间相差较久所以去重新学习了相关的api文档,最后和python的flask web相融合做出了这个产品。 - 高德API功能的调用 通过[高德API开放平台](https://lbs.amap.com/) 的 **[“地理编码”API](https://lbs.amap.com/api/webservice/guide/api/georegeo)** 和 **[“路径规划”API](https://lbs.amap.com/api/webservice/guide/api/direction#walk)** 以及结合 **python 的 flask Web App** 进行功能开发和优化 ### 2. 用到的基本的Python基础知识点 - 文本函数的封装,模块的调用 - if else与for循环语句的使用 - 变量定义 - 参数传递 - 嵌套字典的取值 - 列表元素的添加 - flask web的使用 # 四、编程功能的基本描述 ### 1. 准备工作 ``` from flask import Flask,jsonify,render_template,request,escape from api import geocode,walking import pandas as pd ``` ### 2. 调用日志记录函数 ``` def log_request(req:'flask_request',res:str)->None: with open('vsearch.log','a') as log: print(req.form,req.remote_addr,req.user_agent,res,file=log,sep='|') ``` ### 3. 进入网页的首页,数据返回homepage.html ``` @app.route('/') def shouye() ->'html': return render_template('homepage.html',the_title='带你走') @app.route('/index',methods=['POST']) def login_page() -> 'html': return render_template('index.html',the_title ="带你走") @app.route('/shouye_xianshi',methods=['POST']) def gongnengye() -> 'html': return render_template('shouye_xianshi.html',the_title ="带你走") ``` ### 4. 点击“搜索”后,调用高德api——geocode,walking,数据返回gongnengye ``` @app.route('/ob',methods=['POST']) def gongneng(): address_origin = request.form['address_one'] origin = geocode(key,address_origin) address_destination = request.form['address_two'] destination = geocode(key,address_destination) results = walking(key,origin,destination)['route']['paths'][0]['steps'] daohang = [] for i in results: daohang.append(i['instruction']) return render_template('gongnengye.html',res=daohang,the_title='您的步行导航:',) ``` ### 5. 查看日志 ``` @app.route('/viewlog') def view_the_log() ->'html': contents = [] with open('vsearch.log') as log: for line in log: contents.append([]) for item in line.split('|'): contents[-1].append(escape(item)) titles = ('Form Data','Remote_addr','User_agent','Results') return render_template('viewlog.html', the_title='View Log', the_row_titles=titles, the_data=contents,) ``` ### 6. 高德API调用的数据准备 ``` import requests import json key="b31da81c9c868d1da2b0a1b7fc1419b4" ``` ### 7. 调用地理编码API ``` def geocode(key,address,city=None,batch=None)->str: """获取高德API的地理编码 注释:key是指高德API的秘钥 address是指结构化地址, 参考链接:https://lbs.amap.com/api/webservice/guide/api/georegeo """ url = "https://restapi.amap.com/v3/geocode/geo?parameters" params = { "key":key, "address":address, "city":city, "batch":batch } response = requests.get(url,params=params) results = response.json()['geocodes'][0]['location'] return results ``` ### 8. 调用步行路径规划API ``` def walking(key,origin,destination,sig=None,output=None,callback=None): """高德API步行路径规划 (origin,destination是所需要转换的坐标点经纬度,output(XML)用于指定返回数据的格式, Key 是高德Web服务 Key。 参考链接:https://lbs.amap.com/api/webservice/guide/api/direction#walk ) """ url = "https://restapi.amap.com/v3/direction/walking?parameters" params = {'key':key, 'origin':origin, 'destination':destination, 'sig':sig, 'output':output, 'callback':callback } r = requests.get(url, params=params) results = r.json() return results ``` ### 9. 实现首页界面 ```
``` ### 10. 实现登录界面 ``` {% extends 'base.html' %} {% block body %}