如何为简单的 Python 脚本创建 Web 界面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4412476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a web interface to a simple python script?
提问by Jonathan
I am learning python. I have created some scripts that I use to parse various websites that I run daily (as their stats are updated), and look at the output in the Python interpreter. I would like to create a website to display the results. What I want to do is run my script when I go to the site, and display a sortable table of the results.
我正在学习蟒蛇。我创建了一些脚本,用于解析我每天运行的各种网站(随着它们的统计数据的更新),并查看 Python 解释器中的输出。我想创建一个网站来显示结果。我想要做的是在访问站点时运行我的脚本,并显示一个可排序的结果表。
I have looked at Django and am part way through the tutorial, but it seems like an awful lot of overhead for what should be a simple problem. I know that I could just write a Python script to output simple HTML, but is that really the best way? I would like to be able to sort the table by various columns.
我已经看过 Django 并且已经完成了教程的一部分,但是对于一个应该是简单的问题来说,这似乎是一个巨大的开销。我知道我可以只编写一个 Python 脚本来输出简单的 HTML,但这真的是最好的方法吗?我希望能够按不同的列对表格进行排序。
I have years of programming experience (C, Java, etc.), but have very little web development experience.
我有多年的编程经验(C、Java 等),但很少有 Web 开发经验。
Thanks in advance.
提前致谢。
采纳答案by Bradley Kreider
If you are creating non-interactive pages, you can easily setup any modern web server to execute your python script as a CGI. Instead of loading a static file, your web server will return the output of your python script.
如果您正在创建非交互式页面,您可以轻松设置任何现代 Web 服务器以将您的 Python 脚本作为 CGI 执行。您的 Web 服务器将返回 Python 脚本的输出,而不是加载静态文件。
This isn't very sophisticated, but if you are simply returning the output without needing browser submitted date, this is the easiest way (scaling under load is a different story).
这不是很复杂,但如果您只是在不需要浏览器提交日期的情况下返回输出,这是最简单的方法(在负载下缩放是另一回事)。
You don't even need the "cgi" module from python, if you aren't receiving any data from the browser. Anything more complicated than this and you should use a web framework.
如果您没有从浏览器接收任何数据,您甚至不需要来自 python 的“cgi”模块。任何比这更复杂的事情,您都应该使用 Web 框架。
Examples and other methods
示例和其他方法
- Simple Example: hardest part is webserver configuration
- mod_python: Cut down on CGI overhead (otherwise, apache execs the python interpreter for each hit)
- python module cgi: sending data to your python script from the browser.
- 简单示例:最难的部分是网络服务器配置
- mod_python:减少 CGI 开销(否则,apache 每次命中都会执行 python 解释器)
- python 模块 cgi:从浏览器向您的 python 脚本发送数据。
Sorting
排序
Javascript side sorting: I've used this javascript library to add sortable tables. This is the easiest way to add sorting without requiring additional work or another HTTP GET.
Javascript 端排序:我使用这个 javascript 库来添加可排序的表。这是添加排序的最简单方法,无需额外工作或其他 HTTP GET。
Instructions:
Download this file
Add to your HTML
Add class="sortable" to any table you'd like to make sortable
Click on the headers to sort
说明:
下载此文件
添加到您的 HTML
将 class="sortable"添加到您想要排序的任何表格
单击标题进行排序
回答by jonesy
You might consider Tornadoif Django is too much overhead. I've used both and agree that, if you have something simple/small to do and don't already know Django, it's going to exponentially increase your time to production. On the other hand, you can 'get' Tornado in a couple of hours and get something relatively simple done in a day or two with no prior experience with it. At least, that's been my experience with it.
如果 Django 开销太大,您可能会考虑Tornado。我已经使用了两者并同意,如果您有一些简单/小的事情要做并且还不了解 Django,它将成倍地增加您的生产时间。另一方面,您可以在几个小时内“获得” Tornado,并在一两天内完成一些相对简单的事情,而无需事先使用它。至少,这是我的经验。
Note that Tornado is still a tradeoff: you get a lot of simplicity in exchange for the huge cornucopia of features and shortcuts you get w/ Django.
请注意,Tornado 仍然是一种权衡:您可以获得很多简单性,以换取使用 Django 获得的大量功能和快捷方式。
PS - in addition to being a 'micro-framework', Tornado is also its own web server, so there's no mucking with wsgi/mod-cgi/fcgi.... just write your request handlers and run it. Be sure to see the demos included in the distribution.
PS - 除了作为一个“微框架”之外,Tornado 也是它自己的 Web 服务器,所以 wsgi/mod-cgi/fcgi 没有任何问题......只需编写您的请求处理程序并运行它。请务必查看发行版中包含的演示。
回答by AndrewF
Have you considered Flask? Like Tornado, it is both a "micro-framework" and a simple web server, so it has everything you need right out of the box. http://flask.pocoo.org/
你考虑过 Flask 吗?与 Tornado 一样,它既是一个“微框架”又是一个简单的 Web 服务器,因此它具有开箱即用的一切所需。http://flask.pocoo.org/
This example (right off the homepage) pretty much sums up how simple the code can be:
这个例子(就在主页上)几乎总结了代码是多么简单:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
回答by Lennart Regebro
Django is a big webframework, meant to include loads of things becaus eyou often needs them, even though sometimes you don't.
Django 是一个大型网络框架,旨在包含大量内容,因为您经常需要它们,即使有时您不需要。
Look at Pyramid, earlier known as BFG. It's much smaller. http://pypi.python.org/pypi/pyramid/1.0a1
看看 Pyramid,早先被称为 BFG。它要小得多。 http://pypi.python.org/pypi/pyramid/1.0a1
Other microframeworks to check out are here: http://wiki.python.org/moin/WebFrameworks
其他要检查的微框架在这里:http: //wiki.python.org/moin/WebFrameworks
On the other hand, in this case it's probably also overkill. sounds like you can run the script once every ten minites, and write a static HTML file, and just use Apache.
另一方面,在这种情况下,它也可能是矫枉过正。听起来您可以每十分钟运行一次脚本,然后编写一个静态 HTML 文件,然后使用 Apache。
回答by Tauquir
回答by Pitto
If I correctly understood your requirements you might find Wooey very interesting.
如果我正确理解了您的要求,您可能会发现 Wooey 非常有趣。
Wooey is a A Django app that creates automatic web UIs for Python scripts: http://wooey.readthedocs.org
Wooey 是一个 Django 应用程序,可为 Python 脚本创建自动 Web UI:http://wooey.readthedocs.org
Here you can check a demo: https://wooey.herokuapp.com/
您可以在这里查看演示:https: //wooey.herokuapp.com/
回答by tiwo
If you don't need any input from the browser, this sounds like an almost-static webpage that just happens to change once a day. You'll only need some way to get html out of your script, in a place where your webserver can access it.)
如果您不需要来自浏览器的任何输入,这听起来像是一个几乎是静态的网页,它恰好每天更改一次。您只需要某种方式从您的脚本中获取 html,在您的网络服务器可以访问它的地方。)
So you'd use some form of templating; if you'll need some structure above the single page, there's static site / blog generatorsthat you can feed your output in, say, Markdown format, and call their make htmlor the like.
所以你会使用某种形式的模板;如果您需要单个页面上方的一些结构,则可以使用静态站点/博客生成器,您可以将其输出为 Markdown 格式,并调用它们make html或类似的格式。
回答by buggy
If you are not willing to write your own tool, there is a pretty advanced tool for executing your scripts: http://rundeck.org/It's pretty simple to start and can be configured for complex scenarios as well. For the requirement of custom view (with sortable results), I believe you can implement a simple plugin for translating script output into html elements.
如果您不愿意编写自己的工具,可以使用一个非常高级的工具来执行您的脚本:http: //rundeck.org/它非常容易启动,也可以针对复杂场景进行配置。对于自定义视图的需求(具有可排序的结果),我相信您可以实现一个简单的插件来将脚本输出转换为 html 元素。
Also, for simple setups I could recommend my own tool: https://github.com/bugy/script-server. It doesn't have tons of features, but very easy for end-users and supports interactive execution.
另外,对于简单的设置,我可以推荐我自己的工具:https: //github.com/bugy/script-server。它没有大量功能,但对最终用户来说非常简单,并且支持交互式执行。

