flask使用模板

flask常用代码模板

from flask import Flask, request
import requests

app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>test</h1>'

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username

@app.route('/go', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['name']
        result = aad()
        return result
    else:
        return 'get'

def aad():
    que = [1, 2, 3, 44, 5, 6, 7, 8] + [0] * 348
    input = [que]
    # print(input)
    url = 'http://localhost:8501/v1/models/aad_serve:predict'
    response = requests.post(url, json={"instances": input})
    # print(response.text)
    return (response.text)

if __name__ == '__main__':
    app.run(debug=True, port=8777)

原文地址:https://www.cnblogs.com/panfengde/p/11231418.html

时间: 2024-07-30 17:39:33

flask使用模板的相关文章

flask渲染模板时报错TypeError: &#39;UnboundField&#39; object is not callable

渲染模板时,访问页面提示TypeError: 'UnboundField' object is not callable 检查代码,发现实例化表单类是,没有加括号:form = NewNoteForm,加了括号后就解决了form = NewNoteForm() @app.route('/index')def index(): form = NewNoteForm notes = Note.query.all() return render_template('index.html', notes

Flask 的模板渲染

Flask 的模板渲染 from flask import Flask,render_template,Markup app = Flask(__name__) app.debug = True USERS = { 1:{'name':'张三','age':18,'gender':'男','text':"道路千万条"}, 2:{'name':'李四','age':28,'gender':'男','text':"安全第一条"}, 3:{'name':'王五','age

Flask之 模板渲染

from flask import Flask, request from flask import render_template app = Flask(__name__) app.debug=True @app.route('/hello/') @app.route('/hello/<name>') def hello(name=None): return render_template('hello.html', name=name) @app.route('/login', meth

flask的模板

flask用的是jinja2的模板 模板其实是一个包含响应文本的文件,其中用占位符(变量)表示动态部分,告诉模板引擎其具体的值需要从使用的数据中获取 使用真实值替换变量,再返回最终得到的字符串,这个过程称为"渲染" Flask是使用 Jinja2 这个模板引擎来渲染模板 使用模板的好处: 视图函数只负责业务逻辑和数据处理(业务逻辑方面) 而模板则取到视图函数的数据结果进行展示(视图展示方面) 代码结构清晰,耦合度低 Jinja2 两个概念: Jinja2:是 Python 下一个被广泛

记录flask使用模板时出现的“Internal Server Error”错误

在看<Flask Web开发实战:入门.进阶与原理解析(李辉著 )>时照着书上的代码抄了一遍,然后运行时发现一直出现以下的错误 书上的源代码如下 watchlist.html <head> <meta charset="utf-8"> <title>{{ user.username }}'s Watchlist</title> </head> <body> <a href="{{ url

flask jinja2模板引擎

from flask import Flask, jsonify,render_templateapp = Flask(__name__) @app.route("/")def hello(): return jsonify({"username":"hello world! Zhang"}) @app.route("/jinja2")def jinja2(): return render_template('jinja2.h

Flask之模板之宏、继承、包含

3.5 宏.继承.包含 类似于python中的函数,宏的作用就是在模板中重复利用代码,避免代码冗余. Jinja2支持宏,还可以导入宏,需要在多处重复使用的模板代码片段可以写入单独的文件,再包含在所有模板中,以避免重复. 定义宏 {% macro input() %} <input type="text" name="username" value="" size="30"/> {% endmacro %} 调用宏

[flask]jinjia2模板-搜索功能

思路: 通过flask-wtf创建forms表单类 在app.py中创建search_name()视图函数,实例化form表单类,将通过render_template将form传给html模板 创建html模板,引用form表单类生成的 文本输入框,搜索按钮 forms.py from flask_wtf import FlaskForm from wtforms import StringField,SubmitField from wtforms.validators import Data

flask web开发笔记 -- 模板

业务(business logic 比如插入数据库)和展示逻辑(presentation logic, 比如生成返回)最好分开,展示逻辑可以放置在模板中.模板是一个包含响应文本的文件,用占位符变量表示动态部分.rendering(渲染):把占位符用实际值代替,并返回最终响应字符串.Flask采用模板引擎Jinja2. 模板引擎Jinja2 简单的模板:templates/index.html <h1>Hello World!</h1> 带参数的模板:templates/user.h