如何在网络浏览器中运行python脚本并处理结果? [英] How can I run my python script from within a web browser and process the results?

查看:162
本文介绍了如何在网络浏览器中运行python脚本并处理结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简短的python脚本,该脚本接受一个文本并执行一些操作。例如,它具有一个功能,可以计算文本中的单词并返回数字。

I have a written a short python script which takes a text and does a few things with it. For example it has a function which counts the words in the text and returns the number.

我如何在django中运行此脚本?
我想从视图中获取文本(文本字段或其他内容),然后将结果返回到视图。

How can I run this script within django? I want to take that text from the view (textfield or something) and return a result back to the view.

我只想使用django该脚本是一个Web界面。它只适合我,也许只适合少数人,而不适合大批观众。没有部署。

I want to use django only to give the script a webinterface. And it is only for me, maybe for a few people, not for a big audience. No deployment.

编辑:当我最初认为解决方案是 Django时,我明确要求它。由于我对WSGI的无知,这当然是一个错误。不幸的是没有人告诉我这个错误。

When I first thought the solution would be "Django", I asked for it explicitly. That was of course a mistake because of my ignorance of WSGI. Unfortunately nobody advised me of this mistake.

推荐答案

首先,您是否真的对Django感兴趣?如果不是这样,我建议Django虽然是一个很棒的框架,但可以满足您的需求。您真的不需要完整的堆栈。

First off, is your heart really set on it being Django? If not I'd advise that Django, whilst an awesome framework, is a bit much for your needs. You don't really need full stack.

您可能想看看 Flask 相反,它是Python的微框架(而且很容易使用)

You might want to look at Flask instead, which is a Python micro-framework (and dead easy to use)

但是,因为您询问了Django ...

However, since you asked about Django...

您可以创建自定义Django命令(此处为文档)调用您的脚本,
可以从

You can create a custom Django command (docs here) that calls your script, that can be called from a view as described in this question.

这还有一个额外的好处,即允许您通过Django运行脚本也是management.py脚本。这意味着您可以使以后与此项目相关的所有脚本保持一致。

This has the added benefit of allowing you to run your script via the Django management.py script too. Which means you can keep any future scripts related to this project nice and uniform.

要获得脚本运行的结果,可以从同一段代码中获取它们。调用命令(最后一个链接中描述的部分),或者您可以将大型结果集写入文件并处理该文件。选择哪种方法实际上取决于结果集的大小以及以后是否要进行其他任何操作。

For getting the results of your script running, you can get them from the same bit of code that calls the command (the part described in the last link), or you can write large result sets to a file and process that file. Which you choose would really depend on the size of your result set and if you want to do anything else with it afterwards.

这篇关于如何在网络浏览器中运行python脚本并处理结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆