1

I want user to enter a sentence then I break up that sentence into a list. I got the html page down but i have trouble passing that sentence to python.

How do I properly send the user input to be processed by python and output it to a new page?

1
  • 1
    Are you using a framework? Code in your questions will really help you find an answer. Commented Mar 2, 2012 at 21:10

2 Answers 2

3

There are many Python web frameworks. For example, to break up a sentence using bottle:

break-sentence.py:

#!/usr/bin/env python
from bottle import request, route, run, view

@route('/', method=['GET', 'POST'])
@view('form_template')
def index():
    return dict(parts=request.forms.sentence.split(), # split on whitespace
                show_form=request.method=='GET') # show form for get requests

run(host='localhost', port=8080)

And the template file form_template.tpl that is used both to show the form and the sentence parts after processing in Python (see index() function above):

<!DOCTYPE html>
<title>Break up sentence</title>
%if show_form:
<form action="/" method="post">
  <label for="sentence">Input a sentence to break up</label>
  <input type="text" name="sentence" />
</form>
%else:
Sentence parts:<ol>
%for part in parts:
     <li> {{ part }}
%end
</ol>
%end

request.forms.sentence is used in Python to access user input from <input name="sentence"/> field.

To try it you could just download bottle.py and run:

$ python break-sentence.py 
Bottle server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Now you can visit http://localhost:8080/.

Sign up to request clarification or add additional context in comments.

4 Comments

cool! i was actually using bottle to do some simple pages but i did not know that i can use %code in the html as well to use python code.. does it have to be .tpl file to have python code or can it be an .html file and bottle would know how to render the results???
@JoeChen: form_template.tpl is not an html file; it is a template. To render it as html bottle uses parts, show_form variables that you pass in the index() function. You can use .html extension but I don't see it mentioned in the docs so it might be unsupported.
this should be my last question: does bottle work with any database/sql? django is just too much for me right now
@Joe Chen: the answer is the same as for "does an arbitrary Python script work with any database/sql?" question (answer: it can work with many). There are plugins such as bottle-sqlite, bottle-sqlalchemy that make it more convenient/allow you to avoid some boilerplate code but it is not necessary to use them.
2

Have you tried Google? This page sums up the possibilities, and is one of the first results when googling 'python html'.

As far as I know, the two easiest options for your problem are the following.

1) CGI scripting. You write a python script and configure it as a CGI-script (in case of most HTTP-servers by putting it in the cgi-bin/ folder). Next, you point to this file as the action-attribute of the form-tag in your HTML-file. The python-script will have access to all post-variables (and more), thus being able to process the input and write it as a HTML-file. Have a look at this page for a more extensive description. Googling for tutorials will give you easier step-by-step guides, such as this one.

2) Use Django. This is rather suited for larger projects, but giving it a try on this level may provide you certain insights, and wetting your appetite for future work ;)

1 Comment

A microframework such as Flask or Bottle might be a better alternative than CGI. The same simplicity but more convenience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.