retrieve local html form - python

I have a html page that contains a form and I would like a user to be able to input some data in the form. I would then retrieve this data with a python script.
I don't understand how to make the html and python communicate. How can the python retrieve the form after the user has clicked on post? Is there a basic example I can use to demonstrate this?

What you need is an HTTP server. Python can be used to make a server, or you could get one such as Apache or Lighttpd. I wrote an example application (Rock, Paper, Scissors) based off of this a while ago: https://dl.dropboxusercontent.com/u/95437705/RPS_Web.tgz

C0deH4cker is right that you should have your python program running somewhere in some kind of web server.
A proper way to do this for larger projects is to use a web framework like Django or Web2py. These frameworks have excellent documentation and tutorial pages that are well worth the effort of consuming. A smaller benefit is that both have their own built-in webserver for development an small deployment.
Another way is to write python code that has a web server of its own. You could have a look at Twisted, the networking framework for python. Good info is at http://twistedmatrix.com/documents/current/web/howto/web-in-60/index.html.
Finally -- but this is considered a bit outdated, I guess (??) -- there is the CGI option you describe. Most web servers like apache and lighttpd have the option to run CGI scripts. CGI scripts are very simple programs that, in their simplest form, when run, output HTML code on their stdout. The webserver looks at the first ("shebang") line in the script to determine the script interpreter, executes the script, picks up the output and serves it over HTTP.
Specific provisions have been made in CGI for parameter passing. In the Python CGI module, these are obtainable through cgi.FieldStorage()
First for some HTML:
An HTML form has an "action" and a "method" attribute. Action is the URL (absolute, or relative to the current path) where it will send the form data to; Method is the method to use, usually for a form with a submit button this is "POST".
So if you have this HTML code running somewhere:
<html>
<body>
<form name="sample" action="cgi-bin/myscript.cgi" method="post">
Name: <input type="text" name="name"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
When the user presses the submit button, all input values are posted to the URL "myprogram.cgi".
A good text on what CGI code should look like is in http://docs.python.org/library/cgi.html .
Some code that should work is:
import cgi
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "<body>\n"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"
form = cgi.FieldStorage()
if "name" not in form:
print "<H1>Error</H1>"
print "Please fill in the name fields"
else:
print "<p>name:", form["name"].value
print "</body>"
Now, it is important that you realize that you have to make this code available on the specified URL from a web server. How you should do this depends on your web server.
On apache under linux, you would normally put the script in /var/www/cgi-bin. Possibly you should enable cgi execution in the apache configuration, as it may be switched off for security reasons.

Related

Running a Python Program in the backend on an HTML button click

I am developing a location based web app/website. The website involves an HTML webpage, which has a button which when clicked by the user should:
Extract the user location coordinates
Run a python Program whose input will be the extracted user location, perform some manipulation.
Finally display real-time results back to the html webpage/ i.e. user computer screen.
The python Program is currently accepting the user location by using geocoder function. The program is successfully running on my system/PC.
I have an AWS EC2 Ubuntu server, to which the location of the vehicle GPS device is being sent.
Finally once everything is set up, I will be hosting/deploying the website so that individual users can test it on their system. I am aware of the hosting part.
Can someone please tell me how do I get along with the task of running the python program on an HTML button click and sending back real time results (which is the output of the python program) back to the html webpage?
To run python code on a buttonClick, you can set a href attribute to a link, which you catch in your flask backend. For example <a href="http://myapp/runthiscode"/> in the HTML and app.rout("/runthiscode") in flask. After that, the manipulation, you can give the updated variables to the page, by redirecting with redirect("/", args=args). args are the updated variables, which you can use in the HTML. For example like this: <a> {{ args }} </a>
The simplest way I can think of doing this is creating a basic API endpoint to do this data processing and return it to your frontend.
The process will look like:
User clicks button
Grab the user's longitutde and latitude (most likely using this, though from the sounds you might be able to do this already)
Submit a POST request to your Flask backend
Render the response in the frontend
Jonas answer gives you a rough sense of what this would look like; the only thing I would add is a tutorial. This one from FreeCodeCamp looks great.

Having trouble getting my http server to recognize and run python scripts

I've created a local address http webserver that will hopefully be able to open both HTML webpages and python scripts that create HTML webpages, but I keep getting a 501 error because I can't figure out how to get the webserver to recognize the code correctly. Another note: I'm coding on Windows, I'm not using Cygwin (I don't know anything about Cygwin, so if it's the recommended method here tips on how to get started with it would be appreciated.)
Anyways, here's what I've got.
I have created a folder in C:\ called server_test, and inside this folder is where I have been putting all my relevant HTML and python files. I've been editing my .py files in PyDev in eclipse (C:\workspace) and then copying the files over and putting them in C:\server_test.
To get my server running, I navigate to \server_test in cmd, open python, then create a webserver using HTTPServer, CGIRequestHandler, the current directory, address 127.0.0.1, and a port of my choosing (usually 9090). Once I've done this, I can go to my web browser and type in the address for one of my HTML pages and it runs perfectly fine. However, I currently have an HTML page meant to call a python script (also located in \server_test) that will create another HTML page, but I can't get it to work.
My HTML code looks like this:
<html>
<title>Debug Page</title>
<body
<h1>This is a test file. </h1>
<form method=POST action="my_code_2.py">
<P><input type=submit>
</form>
</body></html>
And then my 'my_code_2.py' looks like this:
#!C:\Python35-32\python.exe
import cgi
import cgitb; cgitb.enable()
print("Content-type: text/html\r\n\r\n")
print('<html>')
print('<h1>')
print('<title>This is a second test.</title>')
print('</h1>')
print('</body')
print('</html>')
From what I've read about shebang lines, it appears Windows native doesn't support them? So how can I make sure that my computer knows it's supposed to run the code as Python? At the moment, when I press the button on my first HTML page, the page http://127.0.0.1:port/my_code_2.py is merely a white page with my python code printed on it.
Try changing the extension to .cgi on your python file and see if that helps. You'll probably want to use something like bottle.py or django tho if you're running python on a webserver. Bottle is easier to learn but with fewer features.
http://bottlepy.org/docs/dev/index.html
https://www.djangoproject.com/

How do Bottle and HTML communicate back and forth?

So I am attempting to use bottle.py and twitter bootstrap together to make a small website. I need to be able to insert a reasonable amount of data at various points in the HTML using Python but I am not really sure I understand how the HTML and python are communicating.
Here is an example of twitter and bottle working together.
He mentions linking a couple of .js files in the html and I can see where he does that but I am not really sure how that affects how the python interacts with the html. Is there a callback from the Javascript that the python catches using request.GET.get().strip():?
Also one of the last lines is:
return template('templates/gpio.tpl', colour1=colour1, colour2=colour2, colour3=colour3)
I am not sure how the templates/gpio.tpl is connected to the html he mentions below. I understand that the colour# variables are referenced in the html (I assume this happens with the {{}} syntax) but I am not sure how the html gets called at all.
From what I understand (which so far isnt a whole lot) this is how it goes:
User enters "server:port/gpio" into a webbrowser
The python decorator gets called by bottle and the function is run returning the template at the bottom.
This is where I get confused.
A) How does the python script know to call the html?
B) How does the gpio.tpl template code get sent to the html?
C) Is it safe to assume that the python arguements sent to the template function can be referenced using the {{}} syntax or is there more to it?
D) How does the html call back to the python to update the buttons he shows at the bottom?
D.1) does the JS linked at the top have something to do with this?
Lastly: If anyone has a another/better example of linking bootstrap and bottle I would be very happy to see it.
This is quite a loaded post. Thank you for your patience. :D
You'd really need to first learn how the HTTP protocol works... But let's try to quickly answer your main question:
how the HTML and python are communicating
Quite simply: they don't. What happens is:
your client (usually your browser) send an HTTP request to your site
the front web server (Apache, Nginx, whatever) sends this request to the bottle.py application
the bottle.py app dispatch the request to the right controller function matching the url's path portion and request's method against the defined routes)
the controller function does what it has to do and returns an HTTP response to the front web server
the front web server send this response to your client
Usually - but not necessarily - the HTTP response contains HTML content, generated by the controller using a template. IOW : bottle.py uses the template to generate html that is sent back to the client.
Once the response is sent, there's no more "communication" until the client sends another request.
So if I wanted to make an html button that changed something on the page how would I send that response back to bottle.py regenerate the page with the change?
It depends on what you want to change...
For example, pushing the button could trigger a new HTTP request, you should then define a new feature in your code.
Let's take the previous example, and imagine you want to add a switchOff button.
You have to add the following button somewhere in the gpio.tpl :
<input type="submit" class="btn" name="LedsOff" value="Turn off the leds!">
Then, modify the function gpio() to add a new condition with the following :
elif request.GET.get('LedsOff','').strip():
from quick2wire.gpio import Pin, exported
with exported(Pin(12, Pin.Out)) as out1, exported(Pin(13, Pin.Out)) as out2:
out1.value = 0
out2.value = 0

Executing Python From HTML on Generic Web Platform

So I recently discovered mod_python, which is great because its not hard to rewrite the PHP code I had and execute through Apache. However, I just found out that my hosting service, (HostGator), does not support this module because they have a single apache user instance per physical shared server.
This begs the question:
Without the use of something like Django, Pylons, etc, is there a way to invoke python routines server side from HTML code and have it return web content?
Example:
Simple form
<form name="message" action="directory/pythonscript.py/function" method="POST">
where, upon submission, the form parses the content in the form, and then the python routine returns a string of the newly assembled HTML file based on the input of the form, and the browser presumably re-renders it.
def function(args):
#parse inputs
#assemble html string
return htmlString
Additional question:
With mod_python, you must pass at least the "req" arg, which points to the apache request structure. Is this functionality still present without mod_python? Would I be able to somehow physically map to that without mod_python?
I'm presuming that all of this is possible because HostGator does support python and a lot of the modules, smtpd for example, have functionality to send emails, communicate over the web, etc, so presumably there is some way to access this API independent of Apache???
99% of searches for this return info about Django, Pylons, etc. If that is indeed the best way to do it, I will explore that option, but all I want to do for now is create some dynamic HTML content with a back end python handler without having to install any other APIs.
Thanks.
EDIT
I should clarify that I am not attached to mod_python at all, it is just something I found. My question is geared towards avoiding non-standard, (standard = shipped with Python 2.7.x, useable through apache or otherwise), APIs if possible.
After some digging, it seems that the quickest way to do this is through php, but not as per the previous answer.
In my HTML I should have:
<html> <form name="form" method="$POST" action="formAction.php" </html>
in formAction.php:
<?
system( 'python ~/scripts/pythonscript.py/function', $return)
echo $return
?>
where $return is the dynamically generated HTML page string. Within the python script, I'll have to parse the posted values from the HTML session somehow. I think that answers my question but I'll wait and see if someone else posts something more interesting.
EDIT: Figured out how to parse the POSTed values:
<?php
$VAR1 = $_GET["var1"];
$VAR2 = $_GET["var2"];
system("python <script>.py -args=[".$VAR1.",".$VAR2"."]", $return)
echo $return
?>
for example.
You could do a php.execute('pythonScript.py') on submit, right?

CGI & Python - return choice to python script

I have a python script that, once executed from command line, performs the needed operations and exit. If, during the execution, the program is not able to perform a choice, he prompts the user and asks them to take a decision!
Now I have to implement a web interface, and here comes the problems ... I created an htm file with a simple form that, once the user "submits" he passes the parameters to a cgi script that contains just one line and runs my python program ! And it seems to work.
My question is: if it happens that the program needs to ask the user for a choice, how can I return this value to my python script? To prompt the user for a choice I need to create a webpage with the possible choices ... Does anybody know how can I open a webpage with python ?
The second and most important question is: how can I return a value from a web page to my "original" python module? In python I would simply make a
return choice
but with a web page I have no idea how to do it.
Recap:
Starting from a web page, I run a cgi script ! Done
This CGI script runs my python program... Done
If the program is not able to take a decision,
3a create a web page with the possible choices I can do it
3b display the created web page ????????
3c return the response to the original python module ????????
"Does anybody know how can I open a webpage with python ? The second and most important question is: how can I return a value from a web page to my "original" python module ??"
This is all very simple.
However, you need to read about what the web really is. You need to read up on web servers, browsers and the HTTP protocol.
Here's the golden rule: A web server responds to HTTP requests with a web page.
The second part of that rules is: A Request is a URL and a method (GET or POST). There's more to a request, but that's the important part.
That's all that ever happens. So, you have to recast your use case into the above form.
Person clicks a bookmark; browser makes an empty request (to a URL of "/") and gets a form.
Person fills in the form, clicks the button; browser POST's the request (to the URL in the form) and gets one of two things.
If your script worked, they get their page that says it all worked.
If your script needed information, they get another form.
Person fills in the form, clicks the button; browser POST's the request (to the URL in the form) and gets the final page that says it all worked.
You can do all of this from a "CGI" script. Use mod_wsgi and plug your stuff into the Apache web server.
Or, you can get a web framework. Django, TurboGears, web.py, etc. You'll be happier with a framework even though you think your operation is simple.
I think you could modify the Python script to return an error if it needs a choice and accept choices as arguments. If you do that, you can check the return value from your cgi script and use that to call the python script appropriately and return the information to the user.
Is there a reason why you can't call the python script directly? I suspect you'd end up with a neater implementation if you were to avoid the intermediate CGI.
What webserver are you using? What cgi language? Perl maybe?
Web pages don't return values, and they aren't programs - a web page is just a static collection of HTML or something similar which a browser can display. Your CGI script can't wait for the user to send a response - it must send the web page to the user and terminate.
However, if the browser performs a second query to your CGI program (or a different CGI program) based on the data in that page, then you can collect the information that way and continue from that point.
Probably easier if you write your cgi in python then call your python script from the cgi script.
Update your script to separate the UI from the logic.
Then it should be relatively easy to interface your script with the (python) cgi script.
For python cgi reference:
Five minutes to a Python CGI
http://docs.python.org/library/cgihttpserver.html
I think first off you need to separate your code from your interface. When you run a script, it spits out a page. You can pass arguments to it using url parameters. Ideally you want to do your logic, and then pass the results into a template that python prints to the cgi.

Categories