How can I run my python script from within a web browser and process the results?


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.

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.

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.

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.

1/24/2014 1:15:22 PM

Accepted Answer

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.

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

However, since you asked about 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.

This has the added benefit of allowing you to run your script via the Django 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.

5/23/2017 12:34:24 PM

What nobody told me here, since I asked about 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow