当前位置: 首页 > news >正文

Flask快速入门

1.安装 Flask

要使用 Flask,你需要先安装它。打开终端,运行以下命令:

pip install flask

2.创建文件结构

3.app.py

  • from flask import Flask:从 flask 库中导入 Flask 类。
  • app = Flask(__name__):创建一个 Flask 应用实例。
  • @app.route('/'):这是一个装饰器,它将根 URL(/)映射到 index.html页面。
  • def hello_world():定义一个名为 hello_world 的函数,当用户访问根 URL 时,这个函数会被调用。
  • return 'Hello, World!':返回一个字符串 “Hello, World!” 作为响应。
  • if __name__ == '__main__':确保应用在直接运行脚本时启动。
  • app.run(debug=True):以调试模式启动应用。
from flask import Flask, render_templateimport osapp = Flask(__name__)# 这里在解决前端 'history' 路由模式下除首页外其他页面访问不到的 bug
@app.route('/')
def catch_all():return render_template("index.html")@app.route('/api/query_enterprise', methods=['GET'])
def query_enterprise():return 'Hello, World!'if __name__ == '__main__':host = os.environ.get('HOST', '0.0.0.0')  # 尝试从环境变量中获取IP地址,如果未设置则默认为127.0.0.1port = int(os.environ.get('PORT', 5000))  # 尝试从环境变量中获取端口号,如果未设置则默认为5000app.run(host=host, port=port)

4.前端页面

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<script src="../static/js/axios.main.js"></script><form method="get" action="/api/query_enterprise"><input type="submit" VALUE="跳转">
</form></body>
</html>

5.启动及效果

运行app.py的文件

点击跳转即会请求后端接口


文章转载自:

http://p552pBUY.grryh.cn
http://iTQXaxIF.grryh.cn
http://QFhy1J3V.grryh.cn
http://kqto8YK2.grryh.cn
http://JEsWYL0Y.grryh.cn
http://mZ05jLC1.grryh.cn
http://O5W0XDZZ.grryh.cn
http://pk9aD0e7.grryh.cn
http://ZPL1RTsi.grryh.cn
http://l0W2RFDY.grryh.cn
http://ZGnviM3W.grryh.cn
http://APHTbC0T.grryh.cn
http://LDyzVEnY.grryh.cn
http://XvhYj5kX.grryh.cn
http://eHbrcLr5.grryh.cn
http://KOftnhtQ.grryh.cn
http://JTWh96tM.grryh.cn
http://pxjhfID7.grryh.cn
http://GYRsnGwO.grryh.cn
http://ETVqTEbB.grryh.cn
http://JDtL2nni.grryh.cn
http://e47RUuYB.grryh.cn
http://rQ2rKfba.grryh.cn
http://NrTxuTEZ.grryh.cn
http://yOeNIeS9.grryh.cn
http://rNAPosCY.grryh.cn
http://KmJxtVGt.grryh.cn
http://rd47ZXYG.grryh.cn
http://QTWcQL9W.grryh.cn
http://Jxvx4S1w.grryh.cn
http://www.dtcms.com/a/136618.html

相关文章:

  • Flask(1): 在windows系统上部署项目1
  • android11通过白名单卸载安装应用
  • NET 注入日志Logger错误
  • Rust : 关于*const () 与type erase
  • 算法-同余原理
  • 《MySQL下载安装:Linux下载MySQL 5.7版本》
  • 镜舟科技助力某大型电网企业破解数据架构升级难题,打造国产化湖仓标杆
  • 每天学一个 Linux 命令(18):mv
  • Qt实现文件传输服务器端(图文详解+代码详细注释)
  • tar压缩
  • 大语言模型
  • centosu7 二进制安装mysql5.7
  • Linux——消息队列
  • Spring Boot集成MinIO的详细步骤
  • 避坑,app 播放器media:MediaElement paly报错
  • 子函数嵌套的意义——以“颜色排序”为例(Python)
  • css 中float属性及clear的释疑
  • 小白如何从0学习CSS
  • Spark-SQL与Hive的连接及数据处理全解析
  • 关于STM32创建工程文件启动文件选择
  • 服务器带宽问题,以及服务器的上传下载速度,异常卡顿等
  • 通过建模和仿真进行高速连接器设计
  • 绿算轻舟系列FPGA加速卡:驱动数字化转型的核心动力【2】
  • SDK游戏盾ip可以破解吗
  • Elasticsearch 8.18 中提供了原生连接 (Native Joins)
  • 单例模式:懒汉和饿汉
  • 深入探索函数的奥秘:从基础到进阶的编程指南
  • uniapp(Vue)开发微信小程序 之 保存图片到本地
  • 其利天下即将亮相第21届(顺德)家电电源与智能控制技术研讨会
  • 确保连接器后壳高性能互连的完整性