Flask之Hello world 详解

embedded/2024/10/11 1:39:27/

**Flask之Hello world 详解
========================**
以下讲解假设你对python有基本了解,熟悉wsgi,以及了解某种python web framework.

from flask import Flask
app = Flask(__name__)@app.route('/')
def hello_world():return "HELLO WROLD"if __name__ == '__main__':app.run(debug=True)
  1. Flask的实例app就是我们的WSGI application.
  2. 创建Flask实例需要指定一个参数,这个参数一般是application的模块名字或者是包名.Flask根据这个参数定位templates,static files等.
  3. route装饰器告诉Flask什么样的请求路径对应这个函数

####Routing
route()装饰器支持变量规则,用<variable_name>表示.还可以制订一个转换器.例如:

@app.route('/user/<username>')
def show_user_profile(username):# show the user profile for that userreturn 'User %s' % username@app.route('/post/<int:post_id>')
def show_post(post_id):# show the post with the given id, the id is an integerreturn 'Post %d' % post_id@app.route('/user/<path:location>')
def show_path(location):return location

有三种转换器:

int	    accepts integers
float	like int but for floating point values
path	like the default but also accepts slashes

####HTTP METHOD

from flask import request@app.route('/login', methods=['GET', 'POST'])
def login():if request.method == "POST":return 'post'else:return 'get'

####Static Files
在package或则module同目录下创建static目录

url_for('static', filename='style.css')

####rendering templates
默认Flask配置JinJia2作为模板引擎,因为他们是一家的.Flask会在templates目录下查找模板文件,如果application是一个module,那么这个templates目录与application同级目录.如果他是一个package:

  • case 1: a module:

    /application.py
    /templates
    /hello.html

  • case 2: a application

    /application
    /init.py
    /templates
    /hello.html

渲染模板使用render_template()

from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):return render_template('hello.html',name=name)

JinJia2 模板的语法和Mako以及django的语法都差不多,可以稍作了解

<!doctype html>
<title>Hello from Flask</title>
{% if name %}<h1>Hello {{ name }}!</h1>
{% else %}<h1>Hello World!</h1>
{% endif %}

####Context locals
先跳过


http://www.ppmy.cn/embedded/125652.html

相关文章

【Java】springboot 项目中出现中文乱码

在刚创建的springboot项目中&#xff0c;出现乱码&#xff0c;跟走着解决一下 1、Ctrl Shift S 打开idea设置&#xff0c;根据图片来&#xff0c;将③④这三个地方都修改为UTF-8 2、返回配置查看&#xff0c;解决

【WebGis开发 - Cesium】三维可视化项目教程---视点管理

目录 引言一、基础功能探索1. 镜头视角获取2. 镜头视角移动 二、进一步封装代码1. 封装hooks函数2. 看下效果3. 如何使用该hooks函数 三、总结 引言 本教程主要是围绕Cesium这一开源三维框架开展的可视化项目教程。总结一下相关从业经验&#xff0c;如果有什么疑问或更好的见解…

虚拟机ip突然看不了了

打印大致如下&#xff1a; 解决办法 如果您发现虚拟机的IP地址与主机不在同一网段&#xff0c;可以采取的措施之一是调整网络设置。将虚拟机的网络模式更改为桥接模式&#xff0c;这样它就会获得与主机相同的IP地址&#xff0c;从而处于同一网段。或者&#xff0c;您可以使用…

HTML5+Css3(背景属性background)

css背景属性 background 1. background-color背景颜色 背景颜色可以用“十六进制”、“rgb()”、“rgba()”或“英文单词”表示 2. background-image:url(路径);背景图片 也可以写成 background:url(); 3. background-repeat背景重复 属性值&#xff1a; - repeat:x,y平铺…

React18新特性

React 18新特性详解如下&#xff1a; 并发渲染&#xff08;Concurrent Rendering&#xff09;&#xff1a; React 18引入了并发渲染特性&#xff0c;允许React在等待异步操作&#xff08;如数据获取&#xff09;时暂停和恢复渲染&#xff0c;从而提供更平滑的用户体验。 通过时…

python redis使用教程

文章目录 安装Redispython安装redis库使用 Python 连接 Redis使用 Redis 实现缓存Redis 中的常用缓存操作Redis 缓存策略发布与订阅事务安装Redis Redis Windows最新安装教程(2024.10.10) python安装redis库 pip install redisE:\Redis-x64-3.2.100>pip install redis W…

前端的全栈混合之路Meteor篇(二):RPC方法注册及调用

在Meteor 3.0中&#xff0c;RPC&#xff08;远程过程调用&#xff09;机制是实现前后端数据交互的重要特性。通过RPC&#xff0c;前端可以轻松调用后端方法&#xff08;Methods&#xff09;并获取数据&#xff0c;而后端的逻辑也可以同步或异步执行并返回结果。本文将详细介绍M…

239. 滑动窗口最大值

最初想法&#xff1a;用hashmap记录窗口中出现的数字的个数&#xff0c;maxNum记录当前窗口的最大数&#xff0c;当窗口滑动后左侧数个数减一&#xff0c;右侧数个数加一&#xff0c;同时查看原最大数的个数是否为0&#xff0c;如果为0&#xff1a;遍历当前hashmap中的key找到最…