首页 > 编程语言 >用python编写web 界面可以用哪些库

用python编写web 界面可以用哪些库

时间:2024-09-05 09:27:38浏览次数:3  
标签:web square 界面 python greet echo Flask template interface

背景:

很多人熟悉python, 但不熟悉前端语言js, 为了项目快速落地,也不太想去专门学习 React/Angular/Vue这些框架,那么就会问一个问题,能不能用Python直接写出一个简单web界面呢?答案是可以的,而且有多种框架可以用。常见的有下面的几种,可自行搜索学习,选用适合自己的

 

Plotly Dash, 基于Flask,Plotly.js。 画出来的图标很漂亮,尤其是交互式图标很炫酷。 适用于数据展示

Streamlit,也比较常用

Panel, 感觉比Steamlit 少见一些

Gradio, 做单页网页很方便,而且自带 rest api, 如果要做多页网页,就要配合 flask这些框架(下面是用flask+Gradio写的一个多网页demo)

from flask import Flask, render_template_string
import gradio as gr

app = Flask(__name__)

# Define the first Gradio interface for the "/greet" API endpoint
def greet(name):
    return f"Hello, {name}!"

greet_interface = gr.Interface(fn=greet, inputs="text", outputs="text")

# Define the second Gradio interface for the "/square" API endpoint
def square(x):
    return x ** 2

square_interface = gr.Interface(fn=square, inputs="number", outputs="number")

# Define the third Gradio interface for the "/echo" API endpoint
def echo(text):
    return f"You said: {text}"

echo_interface = gr.Interface(fn=echo, inputs="text", outputs="text")

# HTML template for embedding Gradio apps in iframes
iframe_template = '''
    <h1>{{ title }}</h1>
    <iframe src="{{ gradio_url }}" width="100%" height="800px" frameborder="0"></iframe>
'''

# Flask route for the homepage
@app.route('/')
def index():
    return "Welcome to the multi-page Gradio app!"

# Flask route for the "/greet" page
@app.route('/greet')
def greet_page():
    return render_template_string(iframe_template, title="Greet Page", gradio_url=greet_interface.local_url)

# Flask route for the "/square" page
@app.route('/square')
def square_page():
    return render_template_string(iframe_template, title="Square Page", gradio_url=square_interface.local_url)

# Flask route for the "/echo" page
@app.route('/echo')
def echo_page():
    return render_template_string(iframe_template, title="Echo Page", gradio_url=echo_interface.local_url)

if __name__ == '__main__':

    greet_interface.launch(prevent_thread_lock=True)
    square_interface.launch(prevent_thread_lock=True)
    echo_interface.launch(prevent_thread_lock=True)
    # Run the Flask app
    app.run()

 

标签:web,square,界面,python,greet,echo,Flask,template,interface
From: https://www.cnblogs.com/mashuai-191/p/18397690

相关文章

  • 【web安全】横向越权,纵向越权
    纵向越权纵向越权,‌也称为垂直越权,‌是指不同级别或不同层次的用户、‌系统或组件之间,‌未经授权地访问或操作更高级别或更低级别的资源或数据。‌这通常涉及到权限的升级或降级使用。‌示例:在一个学校管理系统中,老师可以审批班级学生的请假申请。学生可以为自己提交请假申......
  • python2
    /转义字符,告诉python输入的符号就是符号本身,没有其他的含义join函数输入字符串,需要用str()进行转换功能:join:用于将一个可迭代对象(如列表或元组)中的元素连接成一个字符串。它需要一个字符串作为分隔符。append:用于向列表的末尾添加一个元素。它直接修改原列表。......
  • 【Python学习笔记】 第2章 Python如何运行程序
    Python解释器简介我们在Python的官网下载并安装Python后,它会生成一些组件,包括解释器和一套支持库。Python的代码必须在解释器中运行。程序运行程序员的视角一个Python程序仅是一个包含Python语句的文本文件。例:创建script0.py文件,并写上:print("Hello,world!")print(2**1......
  • 解决在.net8 WebAPI中 AOP 使用AbstractInterceptorAttribute
    在网上找了许多例子但是放在.net8就不好使了比如在Program中配置IInterceptor或者 services.ConfigureDynamicProxy,网上说的对但是也不全对//通过单元测试(MSTest)//创建IServiceCollectionIServiceCollectionservices=newServiceCollection(); 是能调用Abstr......
  • 每天五分钟深度学习:广播机制(以python语言为例)
    本文重点因为向量化的计算方式导致效率的提升,所以现在很多时候,我们都是用向量化的计算,但是向量化计算有一个问题让人头痛就是维度的问题,本节课程我们将讲解python中的广播机制,你会发现这个机制的优秀之处。代码实例importnumpyasnpa=np.random.randn(3,4)b=np.random.r......
  • 2024年 Windows Python 下载、安装教程,附详细图文
    大家好,今天为大家带来的是2024年WindowsPython下载、安装教程,附详细图文,适用于Python3所有版本,包括Python3.7,Python3.8,Python3.103.9,Python3.10等版本。希望对大家有所帮助Python目前已支持所有主流操作系统,在Linux,Unix,Mac系统上自带Python环境,一般默认装的是Py......
  • 【python】本地local_setting设置与上传仓库
    1.我们在开发过程中,有的配置数据不希望别人看到,例如:数据库信息2.如是我们可以在本地配置一个local_setting.py文件2.1:在setting中最后,导入local_settingtry:from.local_settingimport*exceptImportError:pass2.2:将自己的配置数据,写入到local_setting......
  • 【Python使用】嘿马头条项目从到完整开发教程第8篇:缓存,多级缓存【附代码文档】
    本教程的知识点为:简介1.内容2.目标产品效果ToutiaoWeb虚拟机使用说明数据库理解ORM作用思考:使用ORM的方式选择数据库SQLAlchemy操作1新增2查询all()数据库分布式ID1方案选择2头条使用雪花算法(代码toutiao-backend/common/utils/snowflake)数据库Red......
  • Python基础知识-8(PyQt GUI开发,输出乱码处理)
    (目录)介绍一个VSCode轻量级RestAPI客户端插件:ThunderClient一、关于shebang明确指定解释器#!/usr/bin/python3在shell中寻找第一个python解释器#!/usr/bin/envpython3二、Python类的私有方法/属性Python不支持私有方法/属性,但可以将类成员方法/属性名定义为......
  • 【python】一篇搞懂多解释器与虚拟环境
    一.同一PC上,python同时支持多个版本的解释器共存1.1:安装不同版本python解释器(参考百度资料)#python安装后,解释器目录介绍C:\python39-python.exe[解释器]-Scripts-pip.exe-pip3.exe-pip3.9.exe-Lib-re.py-random.py内......