开发者

Is it possible to use a web page as a user interface to a program written in python, running locally, without a web server?

开发者 https://www.devze.com 2023-04-12 12:52 出处:网络
I have a program written in python, and I would like to make it easy to enter parameter values for this program through a GUI. I realise that I could create a GUI using python tools, but I am interest

I have a program written in python, and I would like to make it easy to enter parameter values for this program through a GUI. I realise that I could create a GUI using python tools, but I am interested in using a html / javascript page and have the javascript code call my python script when the user clicks a button to run. Something like;

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "../scripts/python_script.py", true);
xmlhttp.send();

Currently, when I do that, I just get back the text in the python script, but it doesn't actually run. Ideally the python script would run in the background without blocking further input to the web page, and as the script produces it's different result files (png images), these would be displayed in the browser. Clearly, I could do this using a web server (and I may end up doing this eventually anyway, hence the html interface), but I am wondering if it is possible to do so without one. That way I could package the html page and the python script together and give them to someone who could then go and run the program on their computer without needing to start a web server. Is this possible?

If it is not, is there an alternative way do achieve a similar result? Could I embed a small server into a python script that displays the html page when it starts up, and then responds to an XMLHttpRequest to start the python script? If I did this, would the user have to start the script, and then go to the specified address in their browser as a separate action?

EDIT: I got a quick solution working using SimpleHTTPServer, but I had a look at bottle a开发者_如何学Gond I'll probably try something using that as well. Thanks for your help.


First of all, using something like bottle it is pretty simple to make a web server to run your script. Look at http://bottlepy.org/docs/dev/

A good starting point is the code at http://bottlepy.org/docs/dev/tutorial.html#http-request-methods but you would put up a form asking for parameters rather than a login form. Then just run your Python script, capture the output and send it back in the return statement.

This question Capture subprocess output shows you two ways to run your main script depending on whether you want to show the output progressively or all at the end.


You'd need to bundle some kind of webserver with the application. If it is not intended for deployment I would go for something like bottle.py. It's a micro web framework that has its own development server. Other micro/mini frameworks probably pack their own webserver with them for development purposes (web2py, flask, ..). If you want something more serious you'd probably need some better web server. If that's the case - have a look at this reddit discussion.

0

精彩评论

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

关注公众号