CGI & Python - return choice to python script - python

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.

Related

How can one create an open web interface to a Python script that accepts and returns text?

I have a Python script that accepts text from a user, interprets that text and then produces a text response for that user. I want to create a simple web interface to this Python script that is accessible to multiple people at once. By this I mean that person A can go to the website for the script and begin interacting with the script and, at the same time, person B can do the same. This would mean that the script is running in as many processes/sessions as desired.
What would be a good way to approach this?
Maybe you should try a python web framework like django or flask .etc
Make a simple website that offers a webpage that contians a form to input text ,and when people visit the url, put their text in the form and submit, your code can handle it and return a webpage to show the result.

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

How can I make a simple firefox extension which can call a python script?

There is a website that I frequently go to and play a browser game. I want to be able to have some kind of firefox plugin that can scrape data off of the page and send it to a python script. I want the controls for the program (toggle on/off) to be a HTML display which is added on to the webpage after every time I load it.
I am aware of plugins like Greasemonkey, but I don't want to use this because if I want to send any data to python, I have to setup a python http server and manually launch it every time I want to use my program.
Essentially this is what I want to be able to do:
Open Firefox as I would normally to do any kind of internet browsing
Go to the website which has my game.
The game is loaded, javascript code is executed which adds some basic HTML controls which can be used to toggle settings in my backend python program
If I choose to enable the program, javascript will parse the page when necessary and send that data to a python script on my machine.
The python program executes, recieves the data, and does what I want.
I feel like this should be a simple task, but I can't find anything straightforward. From what I have been reading, I can make a Firefox extension which can do this, but the tutorials I have seen are all for things like adding extra features to the browser. I just want a minimal tutorial since all I need to do is just run my own javascript when visiting website "X" and then call a python script.

Execute .py programme from HTML button

I want to execute a python programme whenever the user clicks on any of the buttons from the django submit_line.html page.
I have tried using variations of the following:
onclick="python script.py"
onclick="/usr/bin/python /home/django/project/script.py"
onclick="python ../../script.py"
but I can't get the script to run.
Does anyone know of any solutions to my problem?
Thanks for your time.
If instantly: use javascript.
If by refreshing the page: just use requests and write your Python code as function in views.py
You don't seem to have understood anything from the Django tutorial or documentation.
In Django, you access the server-side functionality via URLs, which are served by views. You don't reference a Python script directly in your template, you reference a URL which maps to a view, and your Python code goes in that view.
Quite apart from that, onclick is Javascript. You can't just reference a Python script, that makes no sense. You can write a Javascript function to call (via Ajax) a URL which does what you want, or more simply make it a link to that URL.

How to use a python script on a django website

Am new to Django but want to learn it and have covered pretty much the basics on the Django website.
Here is my problem:
I have written a python script which presently works in the python shell, but I want to make use of the script on my web. So that when a user goes to my website and provides the neccesary input, clicks submit, the webpage links the input to the python script(which already has input fields like those on the webpage), and the python script runs according to the input given by the user, evaluates it and prints the result of the script on the webpage.
Please help me out guys, counting on you all.
But feel free to suggest other frameworks that could best serve my problem.
It sounds like you would be better off moving the contents of your script into a django view and executing it after your form has been validated.
https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#using-a-form-in-a-view
In your view, you can get the form data using request object. You can pass the form values to your script and send output to your response object.
It seems you didn't completely understand how django works. Please go through the tutorial again.

Categories