是否可以使用网页作为用 python 编写的程序的用户界面,在本地运行,无需 Web 服务器?

发布于 2024-12-10 05:12:07 字数 794 浏览 2 评论 0原文

我有一个用 python 编写的程序,我希望通过 GUI 可以轻松地输入该程序的参数值。我意识到我可以使用 python 工具创建 GUI,但我对使用 html / javascript 页面感兴趣,并在用户单击按钮运行时让 javascript 代码调用我的 python 脚本。像这样的东西;

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

目前,当我这样做时,我只是取回 python 脚本中的文本,但它实际上并没有运行。理想情况下,Python 脚本将在后台运行,而不会阻止对网页的进一步输入,并且当脚本生成不同的结果文件(png 图像)时,这些文件将显示在浏览器中。显然,我可以使用网络服务器来做到这一点(无论如何,我最终可能会这样做,因此是 html 界面),但我想知道是否可以在没有网络服务器的情况下做到这一点。这样我就可以将 html 页面和 python 脚本打包在一起,并将它们提供给某人,然后某人可以在自己的计算机上运行该程序,而无需启动 Web 服务器。这可能吗?

如果不是,是否有其他方法可以达到类似的结果?我可以将一个小型服务器嵌入到 python 脚本中,在启动时显示 html 页面,然后响应 XMLHttpRequest 来启动 python 脚本吗?如果我这样做,用户是否必须启动脚本,然后作为单独的操作转到浏览器中的指定地址?

编辑:我使用 SimpleHTTPServer 得到了一个快速的解决方案,但我查看了 Bottle,我可能也会尝试使用它。感谢您的帮助。

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 and I'll probably try something using that as well. Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-12-17 05:12:07

首先,使用像 bottle 这样的东西来创建一个 Web 服务器来运行你的脚本是非常简单的。查看 http://bottlepy.org/docs/dev/

代码是一个很好的起点在 http://bottlepy.org/docs/dev/tutorial.html#http-request-methods 但您会提出一个要求参数的表单而不是登录表单。然后只需运行 Python 脚本,捕获输出并将其在 return 语句中发送回来。

这个问题捕获子进程输出向您展示了两种运行主脚本的方法,具体取决于您是否想要显示逐步输出或最后全部输出。

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.

避讳 2024-12-17 05:12:07

您需要将某种网络服务器与应用程序捆绑在一起。如果它不打算用于部署,我会选择类似 bottle.py 的东西。它是一个微型 Web 框架,拥有自己的开发服务器。其他微型/迷你框架可能会打包自己的网络服务器以用于开发目的(web2py,flask,..)。
如果您想要更严肃的事情,您可能需要一些更好的网络服务器。如果是这种情况 - 请查看 reddit 讨论

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文