# 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. 页面流程图 ![](https://images.gitee.com/uploads/images/2021/0124/193140_495aa036_7543163.png "页面流程图.png") # 二、问题描述 ### 1. 价值主张画布 ![价值画布](https://images.gitee.com/uploads/images/2021/0124/194602_0a62f15a_7543163.png "价值画布.png") ### 2.用户画像 ![用户1](https://images.gitee.com/uploads/images/2021/0124/193240_b91847b3_7543163.png "用户1.png") ![用户2](https://images.gitee.com/uploads/images/2021/0124/195126_bd047264_7543163.png "用户2.png") ![用户3](https://images.gitee.com/uploads/images/2021/0124/195820_7934fa18_7543163.png "用户3.png") # 三、解决方案 ### 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. 实现首页界面 ```

{{the_title}}

点击下方即可直接进入~

``` ### 10. 实现登录界面 ``` {% extends 'base.html' %} {% block body %}

跟我走

可以直接点登录进入

账号:
密码:
{% endblock %} ``` ### 11. 实现步行导航界面 ```

{{the_title}}

温馨提醒:过远距离不适合步行哦

起始点:

如:广东省广州市中山大学南方学院教学楼5

目的地:

如:广东省广州市中山大学南方学院西3

``` # 五、云端项目部署的基本描述 ### 1. pythonanywhere部署基本步骤: **[基于flask框架的webapp在pythonanywhere上的快速部署](https://www.bilibili.com/video/BV127411c7qt?from=search&seid=1434910243403751725)** :arrow_left: :bangbang: ### 2. 页面链接与页面功能介绍 - 首页面,点击进入即可进入登录页面:http://alicecoco777.pythonanywhere.com/ - 登录页面,直接点击登录即可进入能步行导航界面: http://alicecoco777.pythonanywhere.com/index - 输入起始地和目的地,点击搜索即可: http://alicecoco777.pythonanywhere.com/shouye_xianshi - 结果页: ![结果例子](https://images.gitee.com/uploads/images/2021/0124/203214_3ced5654_7543163.png "1.png") ### 3. 数据流程图 ![数据流程](https://images.gitee.com/uploads/images/2021/0124/204228_643ac2c2_7543163.png "数据流程图.png") ### 4. 部署心得 部署时一直不成功,看了多种教程版本不一最后都整懵了,最后重新部署了几次才明白了是文件解压的地方不对最后成功了。 参考视频:[https://www.bilibili.com/video/BV127411c7qt?from=search&seid=1434910243403751725](https://www.bilibili.com/video/BV127411c7qt?from=search&seid=1434910243403751725) # 六、学习/实践心得总结及感谢 - 由于电脑修完之后要重新下载anaconda等程序,有很多一开始迷迷糊糊混过去的地方经过这次期末项目的练习理解的更加透彻,比如为什么anaconda下载之后再桌面没有图标,为什么cmd无法pip install,为什么pycharm里有时没办法直接加例如flask_login等的模块……这些都是在csdn[https://www.csdn.net/](https://www.csdn.net/)上查询到的原因,感谢csdn上的各位大佬。 - 感谢网新的诸位同学们!在做项目的过程中有很多问题之前的同学也有出过,在询问的时候大家都很积极的互帮互助,没有大家的帮助我的期末项目就会更加难熬了。特别感谢我的学习小组“好家伙协会”的各位,大家不只互相帮助代码方面的问题,做项目的过程中也互相鼓励,特别是我的电脑在这段时间不幸进水后又出现各种小问题经常突然死机,写了一半的文档不翼而飞,情绪十分暴躁的时候,是大家一直鼓励我我才能坚持到最后。也很感谢这一整个学期“好兄弟的群”里的课程录屏,大家在重点部分都会录屏上传,课上无法理解的知识经常依靠群里的视频课下再去理解。 - 也很感谢GitHub和gitee的开源代码,我在github上学习了很多flask的项目,虽然最后没有用到但是也受益匪浅。 - 最后要感谢许智超老师这个学期的悉心教导,~~带我们从入门到放弃~~带我们逐渐领略了python的魅力,希望下学期python有关的两门课可以顺利成功的学下去。~~呜呜呜~~ 参考链接: [bilibili:基于flask框架的webapp在pythonanywhere上的快速部署](https://www.bilibili.com/video/BV127411c7qt?t=106) [bilibili:python flask部署网站 flask创建的小型博客网](https://www.bilibili.com/video/BV127411c7qt?t=106) [bilibili:Flask校园留言条小 项目实战](https://www.bilibili.com/video/BV1iE41157Ej) [bilibili:Python小项目](https://www.bilibili.com/video/BV1Ja411c74f?p=9) [使用Bootstrap-Flask集成Bootstrap](https://zhuanlan.zhihu.com/p/39799223) [Bootstrap样式](https://zhuanlan.zhihu.com/p/345112197) [github开源项目-picbed](https://gitee.com/staugur/picbed) [留言板 - SayHello](https://github.com/greyli/sayhello) [图片社交网站 - Albumy](https://github.com/greyli/albumy) [Anaconda安装问题解决](https://blog.csdn.net/m0_46198710/article/details/104345542?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161145997016780299066258%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161145997016780299066258&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-1-104345542.pc_search_result_before_js&utm_term=EnvironmentNotWritableError:%20The%20current%20user%20does%20not%20have%20write%20permissions%20to%20the%20target%20environment.&spm=1018.2226.3001.4187) [flask-login插件的使用](https://blog.csdn.net/aitao6851/article/details/101466136) [SyntaxError: invalid syntax的解决方法](https://blog.csdn.net/qq_42719751/article/details/102535051) [flask的问题](https://blog.csdn.net/qq_41420747/article/details/91992487) [requests问题](https://blog.csdn.net/stone_tomcate/article/details/98596215) [anaconda下载没有图标](https://www.pianshen.com/article/46681113506/) [flask基础教程1](https://blog.csdn.net/sinat_17736151/article/details/83653512?ops_request_misc=&request_id=&biz_id=102&utm_term=Python-Flask%2520bootstrap&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-8-83653512.pc_v1_rank_blog_v1) [flask基础教程2](https://blog.csdn.net/sinat_17736151/article/details/83752483) [flask基础教程3](https://blog.csdn.net/sinat_17736151/article/details/84195245) [项目例子](https://blog.csdn.net/findhappy117/article/details/79377416?ops_request_misc=&request_id=&biz_id=102&utm_term=python%2520flask&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-9-79377416.pc_search_result_before_js) [部署anywhere](https://www.jianshu.com/p/9974701034ef?from=singlemessage&isappinstalled=0)