开发者

How do I create a web interface to a simple python script?

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.

I have looked at Django and am part way through the tutorial, but it seems开发者_C百科 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.

I have years of programming experience (C, Java, etc.), but have very little web development experience.

Thanks in advance.


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/

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()


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.

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.

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.

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.

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


You might consider Tornado if 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.

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.

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.


Have you seen bottle framework? It is a micro framework and very simple.


If I correctly understood your requirements you might find Wooey very interesting.

Wooey is a A Django app that creates automatic web UIs for Python scripts: http://wooey.readthedocs.org

Here you can check a demo: https://wooey.herokuapp.com/


Django is a big webframework, meant to include loads of things becaus eyou often needs them, even though sometimes you don't.

Look at Pyramid, earlier known as BFG. It's much smaller. http://pypi.python.org/pypi/pyramid/1.0a1

Other microframeworks to check out are here: 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.


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.

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.


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.)

So you'd use some form of templating; if you'll need some structure above the single page, there's static site / blog generators that you can feed your output in, say, Markdown format, and call their make html or the like.


You can use DicksonUI https://dicksonui.gitbook.io DicksonUI is better Or Remi gui(search in google) DicksonUI is better. I am the author of DicksonUI

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜