python flask 快速搭建 WEB 实战(1)

简介: python flask 快速搭建 WEB 实战(1)

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

1832b220aa754cd18c504acc7686a560.png

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

1832b220aa754cd18c504acc7686a560.png

3. templates配置首页

mkdir templates/index.html

1832b220aa754cd18c504acc7686a560.png

自动创建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不变

运行:

1832b220aa754cd18c504acc7686a560.png

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>

1832b220aa754cd18c504acc7686a560.png

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>

运行:

死的用户

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

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)

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

相关文章
|
1天前
|
数据采集 Web App开发 存储
打造高效的Web Scraper:Python与Selenium的完美结合
本文介绍如何使用Python结合Selenium,通过代理IP、设置Cookie和User-Agent抓取BOSS直聘的招聘信息,包括公司名称、岗位、要求和薪资。这些数据可用于行业趋势、人才需求、企业动态及区域经济分析,为求职者、企业和分析师提供宝贵信息。文中详细说明了环境准备、代理配置、登录操作及数据抓取步骤,并提醒注意反爬虫机制和验证码处理等问题。
打造高效的Web Scraper:Python与Selenium的完美结合
|
4天前
|
存储 数据采集 数据库
Python爬虫实战:股票分时数据抓取与存储
Python爬虫实战:股票分时数据抓取与存储
|
28天前
|
运维 Shell 数据库
Python执行Shell命令并获取结果:深入解析与实战
通过以上内容,开发者可以在实际项目中灵活应用Python执行Shell命令,实现各种自动化任务,提高开发和运维效率。
56 20
|
1月前
|
测试技术 数据库 Python
Python装饰器实战:打造高效性能计时工具
在数据分析中,处理大规模数据时,分析代码性能至关重要。本文介绍如何使用Python装饰器实现性能计时工具,在不改变现有代码的基础上,方便快速地测试函数执行时间。该方法具有侵入性小、复用性强、灵活度高等优点,有助于快速发现性能瓶颈并优化代码。通过设置循环次数参数,可以更准确地评估函数的平均执行时间,提升开发效率。
106 61
Python装饰器实战:打造高效性能计时工具
|
1月前
|
JSON 安全 中间件
Python Web 框架 FastAPI
FastAPI 是一个现代的 Python Web 框架,专为快速构建 API 和在线应用而设计。它凭借速度、简单性和开发人员友好的特性迅速走红。FastAPI 支持自动文档生成、类型提示、数据验证、异步操作和依赖注入等功能,极大提升了开发效率并减少了错误。安装简单,使用 pip 安装 FastAPI 和 uvicorn 即可开始开发。其优点包括高性能、自动数据验证和身份验证支持,但也存在学习曲线和社区资源相对较少的缺点。
84 15
|
2月前
|
数据采集 存储 XML
python实战——使用代理IP批量获取手机类电商数据
本文介绍了如何使用代理IP批量获取华为荣耀Magic7 Pro手机在电商网站的商品数据,包括名称、价格、销量和用户评价等。通过Python实现自动化采集,并存储到本地文件中。使用青果网络的代理IP服务,可以提高数据采集的安全性和效率,确保数据的多样性和准确性。文中详细描述了准备工作、API鉴权、代理授权及获取接口的过程,并提供了代码示例,帮助读者快速上手。手机数据来源为京东(item.jd.com),代理IP资源来自青果网络(qg.net)。
|
2月前
|
存储 数据采集 人工智能
Python编程入门:从零基础到实战应用
本文是一篇面向初学者的Python编程教程,旨在帮助读者从零开始学习Python编程语言。文章首先介绍了Python的基本概念和特点,然后通过一个简单的例子展示了如何编写Python代码。接下来,文章详细介绍了Python的数据类型、变量、运算符、控制结构、函数等基本语法知识。最后,文章通过一个实战项目——制作一个简单的计算器程序,帮助读者巩固所学知识并提高编程技能。
|
2月前
|
小程序 开发者 Python
探索Python编程:从基础到实战
本文将引导你走进Python编程的世界,从基础语法开始,逐步深入到实战项目。我们将一起探讨如何在编程中发挥创意,解决问题,并分享一些实用的技巧和心得。无论你是编程新手还是有一定经验的开发者,这篇文章都将为你提供有价值的参考。让我们一起开启Python编程的探索之旅吧!
65 10
|
3月前
|
开发者 Docker Python
从零开始:使用Docker容器化你的Python Web应用
从零开始:使用Docker容器化你的Python Web应用
110 1

热门文章

最新文章

  • 1
    打造高效的Web Scraper:Python与Selenium的完美结合
    13
  • 2
    Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
    26
  • 3
    AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
    20
  • 4
    【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
    54
  • 5
    部署使用 CHAT-NEXT-WEB 基于 Deepseek
    342
  • 6
    【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
    26
  • 7
    java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
    40
  • 8
    零基础构建开源项目OpenIM桌面应用和pc web- Electron篇
    28
  • 9
    【01】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-硬件设备实时监控系统运营版发布-本产品基于企业级开源项目Zabbix深度二开-分步骤实现预计10篇合集-自营版
    22
  • 10
    FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
    55
  • 推荐镜像

    更多