展示HN:Func-to-web – 将Python函数转化为零样板代码的网页用户界面

1作者: offerrall大约 1 个月前原帖
我需要一个工具来快速原型化我们公司的内部工具——PDF生成器、数据处理器、文件转换器,以便非技术同事也能使用。 func-to-web 可以从 Python 函数签名生成网页用户界面,且无需任何样板代码: ```python from func_to_web import run def divide(a: int, b: int): return a / b run(divide) # 网页用户界面位于 localhost:8000 ``` 对于更复杂的需求,它支持高级类型: ```python def process_data( scores: Annotated[ list[Annotated[int, Field(ge=0, le=100)]], Field(min_length=3, max_length=10) ], # 3-10 项,每项 0-100 files: list[ImageFile], # 多文件上传 email: Email, # 内置验证 notes: str | None = None # 可选字段,带切换 ): return FileResponse(pdf_data, "report.pdf") # 自动下载 run(process_data) ``` 主要特点: - *高级列表*:动态添加/删除,限制列表大小和项目 - *可选字段*:`Type | None` 创建切换开关 - *文件处理*:上传/下载带进度条,支持 GB 级文件 - *丰富输出*:自动显示 PIL 图像、matplotlib 图表或可下载文件 - *动态下拉框*:运行时生成的选项来自函数 - *多个函数*:自动生成索引页面 并不是想取代完整的网页框架,但对于内部工具和快速实用程序,这种方法比编写 HTML 表单更具优势。 已有 454 个单元测试。每天用于内部工具和快速原型开发。第一周获得了 150 个星标。 GitHub: [https://github.com/offerrall/FuncToWeb](https://github.com/offerrall/FuncToWeb) 在这里分享,希望其他人也会觉得有趣 :)
查看原文
I needed a tool to quickly prototype internal utilities for our company – PDF generators, data processors, file converters that non-technical colleagues could use.<p>func-to-web generates web UIs from Python function signatures with zero boilerplate:<p>```python from func_to_web import run<p>def divide(a: int, b: int): return a &#x2F; b<p>run(divide) # Web UI at localhost:8000 ```<p>For more complex needs, it supports advanced types:<p>```python def process_data( scores: Annotated[ list[Annotated[int, Field(ge=0, le=100)]], Field(min_length=3, max_length=10) ], # 3-10 items, each 0-100 files: list[ImageFile], # Multiple file uploads email: Email, # Built-in validation notes: str | None = None # Optional with toggle ): return FileResponse(pdf_data, &quot;report.pdf&quot;) # Auto-download<p>run(process_data) ```<p>Key features: - *Advanced lists*: Dynamic add&#x2F;remove with constraints on both list size and items - *Optional fields*: `Type | None` creates toggle switches - *File handling*: Upload&#x2F;download with progress bars, supports GB+ files - *Rich outputs*: Auto-displays PIL images, matplotlib plots, or downloadable files - *Dynamic dropdowns*: Runtime-generated options from functions - *Multiple functions*: Auto-generates index page<p>Not trying to replace full web frameworks, but for internal tools and quick utilities, this approach feels superior to writing HTML forms.<p>454 unit tests. Used daily for internal tools and rapid prototyping. Got 150 stars in the first week.<p>GitHub: <a href="https:&#x2F;&#x2F;github.com&#x2F;offerrall&#x2F;FuncToWeb" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;offerrall&#x2F;FuncToWeb</a><p>Sharing here in case others find it interesting too :)