# learn_pywebio **Repository Path**: arthuryan669/learn_pywebio ## Basic Information - **Project Name**: learn_pywebio - **Description**: 学习 PyWebIO。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-03-24 - **Last Updated**: 2022-03-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # [PyWebIO](https://pywebio.readthedocs.io/zh_CN/latest/) 像开发 Python 脚本一样,快速构建对 UI 要求不高的临时性应用,无需 HTML 和 JS 知识。 既适合从来没有接触过 Web 开发的程序员来像编写终端程序一样编写 Web 应用,又适合有 Web 开发经验的程序员在自己的 Web 应用中快速实现某些小功能。 打开文档 [User’s guide](https://pywebio.readthedocs.io/zh_CN/latest/guide.html), 在命令行交互式输入代码,会自动弹出浏览器,调整代码观察页面变化。 包含模块: ```shell pywebio.input — 输入模块 pywebio.output — 输出模块 pywebio.session — 会话相关 pywebio.platform — 应用部署 pywebio.pin — 持续性输入 ``` # 案例一: [PyWebIO and MongoDB](https://www.datacrunch.ca/2021/06/18/pywebio-mongodb.html) 一款简单交互式脚本,演示用 PyWebIO 连接 MongoDB 并获取账号列表。 - 安装依赖 ```shell pip install pywebio pymongo dnspython pandas plotly ``` - 修改并运行 [demo_with_login.py](demo01/demo_with_login.py) ```shell 找到函数 put_loading 并修改 Mongo 数据库地址,测试时可关闭授权。 ``` - 浏览器自动弹出登录页面,mongo 服务器授权账号密码 ![demo1_login](demo01/imgs/demo1_login.png) - Mongo 数据库添加账号信息 ```shell > use sample_analytics switched to db sample_analytics > db.customers.insert([ {'accounts': 1001, 'name': 'name 1001'}, {'accounts': 1002, 'name': 'name 1002'} ]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.customers.find() { "_id" : ObjectId("61f37d4ba945236f4ef5ec78"), "accounts" : 1001, "name" : "name 1001" } { "_id" : ObjectId("61f37d4ba945236f4ef5ec79"), "accounts" : 1002, "name" : "name 1002" } ``` - 查询账号信息 ![demo1_lookup](demo01/imgs/demo1_lookup.png) # 案例二: [PyWebIO: Write Interactive Web App in Script Way Using Python](https://towardsdatascience.com/pywebio-write-interactive-web-app-in-script-way-using-python-14f50155af4e) ## 1. 选择菜单,[源码](demo02/output_funcs.py)。 ## 2. 根据输入内容生成 pdf 文件,[源码](demo02/generate_pdf.py)。 ```shell 依赖:pip install fpdf 中文需要字体文件(为测试临时下载的行书字体): font.ttf ``` ![demo_pdf](demo02/demo_pdf.gif) ## 3. 将 CSV 文件转换为可视化图表,[源码](demo02/generate_report.py)。 demo_csf