python flask 快速搭建 WEB 实战
2023-09-11 14:15:45 时间
python flask 快速搭建 WEB 实战
tags: flask
文章目录
视频:https://mp.weixin.qq.com/s/Az1LmhgYjURjsqJW4cBcLg
原创:https://www.youtube.com/watch?v=kng-mJJby8g
github:https://github.com/Ghostwritten/flask_simple_demo.git
python -m pip install flask
mkdir flask_web1
cd flask_web1
mkdir flask_web1/template
mkdir flask_web1/static
touch app.py views.py
1. app.py配置首页
app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "this is the home page"
if __name__=='__main__':
app.run(debug=True, port=8080)
运行
访问:http://127.0.0.1:8080
2. views.py配置首页
app.py
from flask import Flask
from views import views
app = Flask(__name__)
app.register_blueprint(views, url_prefix="/views")
if __name__=='__main__':
app.run(debug=True, port=8080)
views.py
from flask import Blueprint
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return "home page"
运行app.py,此效果调用views.py的blueprint
3. templates配置首页
mkdir templates/index.html
自动创建html模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
</html>
修改:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Home</title>
</head>
<body>
<div><h1>Home Page</h1></div>
</body>
</html>
views.py修改:
from flask import Blueprint, render_template
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return render_template("index.html")
app.py不变
运行:
4. 设置变量
views.py
from flask import Blueprint, render_template
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return render_template("index.html",name="Tim",ago=21)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Home</title>
</head>
<body>
<div><h1>Home Page</h1></div>
<p>hello, {{ name }}, you are {{ ago }} years old!</p>
</body>
</html>
5. 接口变量
views.py
from flask import Blueprint, render_template
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return render_template("index.html",name="Tim")
@views.route("/profile/<username>")
def profile(username):
return render_template("index.html",name=username)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Home</title>
</head>
<body>
<div><h1>Home Page</h1></div>
<p>hello, {{ name }}</p>
</body>
</html>
运行:
死的用户
动态
6. 接口传参
from flask import Blueprint, render_template, request
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return render_template("index.html",name="Tim")
@views.route("/profile")
def profile():
args = request.args
name = args.get('name')
return render_template("index.html",name=name)
7. 接口返回json
views.py
from flask import Blueprint, render_template, request, jsonify
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return render_template("index.html",name="Tim")
@views.route("/profile")
def profile():
args = request.args
name = args.get('name')
return render_template("index.html",name=name)
@views.route("/json")
def get_json():
return jsonify({'name': 'liming','coolness': 10})
8. 接口跳转
views.py
from flask import Blueprint, render_template, request, jsonify,redirect, url_for
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return render_template("index.html",name="Tim")
@views.route("/profile")
def profile():
args = request.args
name = args.get('name')
return render_template("index.html",name=name)
@views.route("/json")
def get_json():
return jsonify({'name': 'liming','coolness': 10})
@views.route("/data")
def get_data():
data = request.json
return jsonify(data)
@views.route("/go-to-home")
def go_to_home():
return redirect(url_for("views.get_json"))
回车go-to-home跳转json
修改为views.py:
return redirect(url_for("views.home"))
go-to-home跳转home
9. index.html添加javascript
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Home</title>
</head>
<body>
<div>
<h1>Home Page</h1>
<p>hello, {{ name }}</p>
</div>
<script type="text/javascript"
src="{{ url_for('static', filename='index.js')}}"
></script>
</body>
</html>
创建index.js
touch static/index.js
index.js写入console日志
console.log("I am running");
运行:
10. 设置风格
touch template/profile.html
profile.html写入风格
{% extends "index.html" %}
{% block content %}
<h1> This is the profile page!</h1>
{% endblock %}
views.py
from flask import Blueprint, render_template, request, jsonify,redirect, url_for
views = Blueprint(__name__, "views")
@views.route("/")
def home():
return render_template("index.html",name="Tim")
@views.route("/profile")
def profile():
args = request.args
name = args.get('name')
return render_template("profile.html")
@views.route("/json")
def get_json():
return jsonify({'name': 'liming','coolness': 10})
@views.route("/data")
def get_data():
data = request.json
return jsonify(data)
@views.route("/go-to-home")
def go_to_home():
return redirect(url_for("views.home"))
运行:
更多阅读:
- linux python web flask 编写 Hello World
- windows python web flask 编写 Hello World
- python flask 快速搭建 WEB
- windows python web flask 模板开发快速入门
- windows python flask 与mysql 数据库写入查询
- windows python flask 返回 json 数据
- windows python flask 读取文件数据转换表格
- python flask template 模板应用
- windows python web route路由详解
- windows python web flask获取请求参数数据
- python flask-caching模块缓存详解
相关文章
- python模块分析之typing(三)
- web自动化测试:Selenium+Python基础方法封装
- python 保存图片_用Python从视频中提取每一帧的图片
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 如何快速学习python,学好python?能通过Python赚到的第一笔钱,有哪些经验可以分享吗?
- 将自己OpenCV-Python-PyCharm开发环境的Python-3.6.8更换为python-3.9.10的详细过程记录
- Python大婶博客汇总
- ASP.NET Web API实践系列06, 在ASP.NET MVC 4 基础上增加使用ASP.NET WEB API
- Python编程-基础知识-python项目包和文件的管理以及如何引用相对路径的包和模块
- Python Web Scraper - Simple Url Request
- Python基础图形绘制库——turtle
- 《Python极客项目编程 》——导读
- 《像计算机科学家一样思考Python》——4.3 练习
- python学习之OpenCV-Python模块的部分应用示例(生成素描图和动漫图)
- Python 教程大全之使用 python 获取唯一单词的计数
- 手把手带你入门Python爬虫(一、工欲善其事必先利其器)
- 华为OD机试 - 投篮大赛Python) | 机试题+算法思路+考点+代码解析 【2023】
- [Spark][Python]spark 从 avro 文件获取 Dataframe 的例子
- python excel文件换题头
- Python 常见问题 之 python 安装包下载安装速度慢 的 快速解决方法(之一)
- python第十四课--排序及自定义函数之案例一:选择排序
- Python语言Web开发框架web2py
- [Python]2分钟完成python + Selenium Web端自动化环境搭建,开启~~~