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.
我有一个简短的python脚本,它接受一个文本并用它做一些事情。例如,它有一个函数可以对文本中的单词进行计数并返回数字。
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中运行此脚本?我想从视图(文本字段或其他东西)中获取该文本,并将结果返回给视图。
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为脚本提供一个web界面。它只适合我,也许适合少数人,不适合大量观众。没有部署。
Edit: 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”时,我明确地要求它。由于我对WSGI的无知,这当然是一个错误。不幸的是没有人告诉我这个错误。
3 个解决方案
#1
7
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.
首先,你的心真的是Django吗?如果不是,我会建议Django,虽然一个很棒的框架,有点满足您的需求。你真的不需要完整的堆栈。
You might want to look at Flask instead, which is a Python micro-framework (and dead easy to use)
你可能想要看看Flask,这是一个Python微框架(并且很容易使用)
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命令(此处为docs),可以从该问题中描述的视图中调用。
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.
这样做的另一个好处是允许您通过Django management.py脚本运行脚本。这意味着您可以保持与此项目相关的任何未来脚本的美观和统一。
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.
为了使脚本运行的结果,您可以从调用命令的相同代码(最后一个链接中描述的部分)中获取它们,或者您可以将大型结果集写入文件并处理该文件。您选择哪种方式实际上取决于结果集的大小,以及之后是否要对其进行任何其他操作。
#2
1
After following the django tutorial, as suggested in a comment above, you'll want to create a view that has a text field and a submit button. On submission of the form, your view can run the script that you wrote (either imported from another file or copy and pasted; importing is probably preferable if it's complicated, but yours sounds like it's just a few lines), then return the number that you calculated. If you want to get really fancy, you could do this with some javascript and an ajax request, but if you're just starting, you should do it with a simple form first.
遵循django教程,如上面的评论所示,您将需要创建一个包含文本字段和提交按钮的视图。在提交表单时,您的视图可以运行您编写的脚本(从另一个文件导入或复制并粘贴;如果复杂,则导入可能更可取,但您的声音只是几行),然后返回数字你算了如果你想得到真正的幻想,你可以用一些javascript和ajax请求来做到这一点,但是如果你刚刚开始,你应该首先使用一个简单的表单。
#3
1
What nobody told me here, since I asked about Django:
自从我问起Django后,没有人在这里告诉我:
What I really needed was a simple solution called WSGI. In order to make your python script accessible from the webbrowser you don't need Django, nor Flask. Much easier is a solution like Werkzeug or CherryPy.
我真正需要的是一个名为WSGI的简单解决方案。为了使你的python脚本可以从webbrowser访问,你不需要Django,也不需要Flask。像Werkzeug或CherryPy这样的解决方案要容易得多。
#1
7
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.
首先,你的心真的是Django吗?如果不是,我会建议Django,虽然一个很棒的框架,有点满足您的需求。你真的不需要完整的堆栈。
You might want to look at Flask instead, which is a Python micro-framework (and dead easy to use)
你可能想要看看Flask,这是一个Python微框架(并且很容易使用)
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命令(此处为docs),可以从该问题中描述的视图中调用。
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.
这样做的另一个好处是允许您通过Django management.py脚本运行脚本。这意味着您可以保持与此项目相关的任何未来脚本的美观和统一。
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.
为了使脚本运行的结果,您可以从调用命令的相同代码(最后一个链接中描述的部分)中获取它们,或者您可以将大型结果集写入文件并处理该文件。您选择哪种方式实际上取决于结果集的大小,以及之后是否要对其进行任何其他操作。
#2
1
After following the django tutorial, as suggested in a comment above, you'll want to create a view that has a text field and a submit button. On submission of the form, your view can run the script that you wrote (either imported from another file or copy and pasted; importing is probably preferable if it's complicated, but yours sounds like it's just a few lines), then return the number that you calculated. If you want to get really fancy, you could do this with some javascript and an ajax request, but if you're just starting, you should do it with a simple form first.
遵循django教程,如上面的评论所示,您将需要创建一个包含文本字段和提交按钮的视图。在提交表单时,您的视图可以运行您编写的脚本(从另一个文件导入或复制并粘贴;如果复杂,则导入可能更可取,但您的声音只是几行),然后返回数字你算了如果你想得到真正的幻想,你可以用一些javascript和ajax请求来做到这一点,但是如果你刚刚开始,你应该首先使用一个简单的表单。
#3
1
What nobody told me here, since I asked about Django:
自从我问起Django后,没有人在这里告诉我:
What I really needed was a simple solution called WSGI. In order to make your python script accessible from the webbrowser you don't need Django, nor Flask. Much easier is a solution like Werkzeug or CherryPy.
我真正需要的是一个名为WSGI的简单解决方案。为了使你的python脚本可以从webbrowser访问,你不需要Django,也不需要Flask。像Werkzeug或CherryPy这样的解决方案要容易得多。