5

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.

Sadık
  • 4,249
  • 7
  • 53
  • 89
  • 2
    The django tutorial is probably a good place to start. – monkut Apr 11 '13 at 22:25
  • Do you have a working django project already or are you starting with nothing? What is your end goal and audience? Is this part of a bigger project or something standalone? Is it just for you or will this be part of a deployed webapp? The answers will drastically change the advice on where you should start. – Collin Green Apr 11 '13 at 22:30
  • I'm sorry but I didn't find the tutorial helpful for that. It is about databases and so on, which I all don't need at the very moment. I didn't see a chapter about processing data which came from a form. – Sadık Apr 11 '13 at 22:59
  • If your question is about processing data from a form, you should ask a more specific question. Are you wondering about how to get data from request.POST, for example? The pointer to the tutorial was in hopes of getting you to the point where you can ask a more answerable question. – mattg Apr 12 '13 at 02:56
  • In response to your edit; I did point out that you probably don't need a full Django project, suggested a simpler alternative (not the simpler alternative you went with, but a simpler one nonetheless) and then answered the question you asked. As it stands right now, your accepted answer doesn't answer the question that was asked (even if it is what you went with in the end). – Tom Manterfield Dec 17 '14 at 14:27
  • 1
    you are right. My accepted answer is the solution for me, not the answer of the question. I will change that. – Sadık Dec 17 '14 at 14:46

3 Answers3

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.

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 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.

Community
  • 1
  • 1
Tom Manterfield
  • 6,515
  • 6
  • 36
  • 52
2

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.

Sadık
  • 4,249
  • 7
  • 53
  • 89
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.

mattg
  • 1,731
  • 1
  • 12
  • 20