# 示例代码:使用 Flask 创建一个简单的 Web UI
from flask import Flask, render_template_string
app = Flask(__name__)
# 定义主页路由
@app.route('/')
def home():
    # 使用内联 HTML 模板渲染页面
    html_content = """
    <html>
        <head>
            <title>Python WebUI Example</title>
        </head>
        <body>
            <h1>Welcome to the Python WebUI Example</h1>
            <p>This is a simple web interface created using Flask.</p>
        </body>
    </html>
    """
    return render_template_string(html_content)
if __name__ == '__main__':
    # 启动 Flask 应用,默认在本地主机的 5000 端口运行
    app.run(debug=True)@app.route('/') 定义了主页的路由。当用户访问根 URL(例如 http://127.0.0.1:5000/)时,会触发 home() 函数。render_template_string() 函数用于将内联的 HTML 字符串作为模板渲染并返回给浏览器。app.run(debug=True) 启动 Flask 应用,并启用调试模式,方便开发过程中快速调试和查看错误信息。这个示例展示了如何使用 Flask 创建一个简单的 Web UI,用户可以通过浏览器访问该页面并看到欢迎信息。
下一篇:python字符串占位符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站