The idea is to have an HTML page that looks like this:

PROJECT1

Send what goes into the web form below to a python script!

 _________________________________________________   ____
| Input some text!                                | | GO |
|_________________________________________________| |____|

When the user hits "GO", the text somehow goes into script.py that does something like the following:

script.py

webformContents = inputtedText # inputtedText comes from the HTML page and is "Hello World" in this example
# do something

So if the user inputted "Hello World", the chain of events (grossly oversimplified, of course) would go like this:

"Hello World" is sent to script.py

webformContents variable is set with the value "Hello World"
the rest of the code in script.py is executed from the value obtained from the web form

What do I need to:

  • do
  • read up on
  • use (do I need my files to be hosted to a web server)? etc.

You could look at making a cgi script, but I don't recommend it. You should look into HTML/JavaScript and AJAX/Forms, and make a Python backend like Flask, Django, or Bottle.

Basically you have a Python app running server-side, that accepts a GET or POST request. A GET request would look like:

http://yoursite.com/?userinput="blah blah"

You need some JavaScript to get the value from your input box. You put the value in some POST data ({userinput: $('#userbox').val()}), or a GET request, send it off from the browser and from there your server app handles it. You'll have to look at the differences between GET and POST. They each have their pros and cons.

In django the view could be as simple as:

def view_helloworld(request):
    userinput = request.REQUEST.get('userinput', None)
    if userinput:
        return HttpResponse('User said: {}'.format(userinput))
    else:
        return HttpResponse('No data!', status=500)

Of course I haven't even mentioned the templating systems available, error handling, or anything else that comes with some of Python's awesome web frameworks.

I use jQuery for AJAX because it simplifies the cross-browser problems. Django has CSRF protection, so you also have to grab the value from the csrftoken cookie and send it along with the data.

I am simplifying things. There is actually a lot more to it. Too much for me to put in a daniweb comment. Look at the things I mentioned, and if you get stuck come back with what you have tried. We will be glad to help you.

Edited 2 Years Ago by chriswelborn: links to resources

As mention bye chriswelborn use Flask or Bottle.
CGI is dead in Python after PEP 3333(WSGI).
Flask,Bottle is a layer above WSGI and 100% WSGI compliant.

You need some JavaScript to get the value from your input box.

JavaScript is not needed for this Flask has of course stuff like this build in with request object.

A quik demo.
HTML form(my_form.html)

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>Enter some text:</h1>
    <h2>Text to be multiplied</h2>
    <form action="." method="POST">
        <input type="text" name="text">
        <input type="submit" name="my-form" value="GO">
    </form>
</body>
</html>

Flask code(form_demo.py)

from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template("my_form.html")

@app.route('/', methods=['POST'])
def my_form_post():
    text = request.form['text']
    multiply_text = text * 3
    return multiply_text

if __name__ == '__main__':
    app.run()

This run fine or your local computer(with Flask installed pip install Flask)
A folder with form_demo.py a subfolder templates with my_form.html
To run it in adress bar browser http://localhost:5000/
If you write hello in input box,you get back hellohellohello in browser.

use (do I need my files to be hosted to a web server)? etc.

As mention to code over work fine without a webserver in browser.
If you want to share it with world you need a webserver.
Look into Python friendly webserver as Heroku,Pythonanywhere,Openshift...

Edited 2 Years Ago by snippsat

@snippsat, you're right about that JavaScript bit. I don't know what I was thinking. I guess if you were trying to do some validation on the client side, besides what the inputs already check, you might need it.

Thank you @snippsat!

I followed your example and have started to see how I can run form_demo.py through the terminal and open up a web browser and do localhost:5000 for the html page to load.

From what I can tell, the directory structure that works with Flask is this:

- [root]
  - templates
    - my_form.html
  - form_demo.py

Now this is great, but now I'd like to get it to where I can share it with the world through Heroku. In fact, I already have a Heroku account and a git repository linked to it.

How would I go about doing something where, if I type:

myapp.heroku.com

It'll take me to my_form.html, upon which after pressing submit, it will run form_demo.py? In other words, how do I make Heroku respect the directory structure laid out that Flask uses?

My git repository looks like this, BTW:

- quick_demo
  - templates
    - my_form.html
  - form_demo.py
- README.md
- download.py
- requirements.txt

Edited 2 Years Ago by amraam

Excellent, thanks for the reply!
Those links were helpful, but what I found most useful was this link:

https://devcenter.heroku.com/articles/getting-started-with-python-o

Because I'd hate to not deliver, here's what I have so far!

(It's supposed to return a downloadable link to an mp3 file pulled from a valid youtube URL which you give it, but for now it only creates an mp4 file and outputs a string of the mp4 file that's been created.)

Edited 2 Years Ago by amraam

This article has been dead for over six months. Start a new discussion instead.