khun
Posts: 13
Joined: Mon Oct 24, 2016 9:03 pm

Web page with python and script

Sat Dec 02, 2023 7:51 pm

Hi again!

I'm still having trouble getting the grasp of how a web page communicates with the server. I am using cgi and have a python script using "print" to create a web page with radio buttons. I have managed (with help from this forum, thank you for that!) to rerun the python code when a radio button is changed, by having:

Code: Select all

    <script>
    function onclick() {
       document.getElementById("roomform").submit();
    }
    </script>
But how can I let the python code know which radio button changed? I can read the values of the radio buttons through FieldStorage, but I don't know if they are changed or not.

User avatar
neilgl
Posts: 7511
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Web page with python and script

Sat Dec 02, 2023 10:40 pm

Can you post all the code so we can look at it?

khun
Posts: 13
Joined: Mon Oct 24, 2016 9:03 pm

Re: Web page with python and script

Sun Dec 03, 2023 1:05 am

Here is a cut down version:

Code: Select all

#! /usr/bin/env python
# coding=utf-8

import os
import serial
import cgi
from subprocess import call
# Some variable initiation left out

form = cgi.FieldStorage()

# How do I here know what radio button was changed, that triggered the submit?

# Output the headers.
print "Content-Type: text/html" # html content to follow
print                           # blank line, end of headers

# Output the content.
print """
<html>
    <script>
    function onclick() {
       document.getElementById("roomform").submit();
    }
    </script>
    <body>
        <form id="roomform" method="post" action="Heat.py">
"""

for room_nr in range(nr_of_rooms):
   print "   <fieldset>"
   print "      <legend>" + rooms[room_nr] + "</legend>"
   print "      <label for=\"room" + str(room_nr) + "On\">On</label>"
   print "      <input type=\"radio\" id=\"room" + str(room_nr) + "On\" name=\"room" + str(room_nr) + "\" value=\"On\" onclick=\"onclick()\"" + (" checked" if room_oven_status[room_nr]=="On" else "") + ">"
   print "      <label for=\"room" + str(room_nr) + "Off\">Off</label>"
   print "      <input type=\"radio\" id=\"room" + str(room_nr) + "Off\" name=\"room" + str(room_nr) + "\" value=\"Off\" onclick=\"onclick()\"" + (" checked" if room_oven_status[room_nr]=="Off" else "") + ">"
   print "   </fieldset>"
   print "   <script>"
   print "   document.getElementById(\"room" + str(room_nr) + "On\").addEventListener(\"click\", onclick, true);"
   print "   document.getElementById(\"room" + str(room_nr) + "Off\").addEventListener(\"click\", onclick, true);"
   print "   </script>"

print """
   </form>
"""

print """
    </body>
</html>
"""

khun
Posts: 13
Joined: Mon Oct 24, 2016 9:03 pm

Re: Web page with python and script

Sun Dec 03, 2023 1:10 am

And to clarify: I am trying to make a web page that lets users change On/Off settings for ovens in the house. So I made a python script that creates a web page, so far so good. But when a user clicks a radio button there, I don't know how to "transmit" that action to the python code.

I don't want to have a "submit" button, but the change to happen at the moment the radio button is changed.

I have managed to get the python code to be rerun when a radio button is changed (by the onclick), but don't see how I can tell the python code what was changed.

ame
Posts: 8733
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: Web page with python and script

Sun Dec 03, 2023 3:10 am

You probably want Python running as a cgi script. In this case you put your script in a special executable directory, and when you call it the web server executes the script with the form contents as parameters. Your script then does whatever (which could be anything, because it's Python), and can use the form content to do different things. Then any output from the script (e.g. print() statements etc.) is sent back to the client by the server. So your script can basically print the HTML for the desired page content.

This might be a good starting point:
https://www.tutorialspoint.com/python/p ... amming.htm
Oh no, not again.

memjr
Posts: 3651
Joined: Fri Aug 21, 2020 5:59 pm

Re: Web page with python and script

Sun Dec 03, 2023 6:12 am

Save yourself the headache and try a web framework like Flask or FastAPI. Either can do what you want with only a few lines of code.

User avatar
neilgl
Posts: 7511
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Web page with python and script

Sun Dec 03, 2023 10:34 pm

Someone remind me which webserver is actually running here - Apache2 / nginx / flask / other ?

memjr
Posts: 3651
Joined: Fri Aug 21, 2020 5:59 pm

Re: Web page with python and script

Mon Dec 04, 2023 4:46 am

khun wrote:
Sun Dec 03, 2023 1:10 am
And to clarify: I am trying to make a web page that lets users change On/Off settings for ovens in the house. So I made a python script that creates a web page, so far so good. But when a user clicks a radio button there, I don't know how to "transmit" that action to the python code.

I don't want to have a "submit" button, but the change to happen at the moment the radio button is changed.

I have managed to get the python code to be rerun when a radio button is changed (by the onclick), but don't see how I can tell the python code what was changed.
The easiest way to explain that is like this .

You go to a url. The web server on the other side gives you a string of bytes, which the browser uses to display something on the screen.

There. It's 100% done. It is static.

All the communication that will ever happen between browser and server happens there. The browser asks for the contents of an address and the server gives the content back.

Clicking something in the browser and making something else happen in the server in response to that click, involves the browser reaching out to the server again, could be the same address or not, but this time with a bit more data to tell it, "button x was clicked". The server then goes, "oh, in that case, then I give you this other thing"

That is what submit does.

But it is still very static.

At some point browsers gained the ability to run code (JavaScript) that the server gave to the browser along with the code that makes the page look like this or like that.

Now, the code in that page executes when the page is displayed. And if you coded it to do x it will do that x when you click the right button.

But what that java script code is doing is more if the same. Instead of submitting data to the server with a submit button which would result in the browser receiving a new set of HTML to refresh the screen with, the java script executed calls the server on its own, without doing anything to the page. And it will also wait for the server to give data back to it. And when it gets that data, it will update something on the page, without the page ever submitting anything.

So it still the same static process, reach out to some address with some optional data or not, and doing something with a response. It's just that the JavaScript way makes it look more dynamic be case the user does not experience an entire new page request.

Now each control on your page that will be actionable needs an id so it can be identified. The javascript is coded so that whatever happens it will tell the server the I'd of the button that you clicked on the page. The server then responds accordingly to what is supposed to happen on that click.

The same thing happens without javascript, the submit button tells the server which button was pressed and the server replied accordingly.

The submit button is just more clunky than the JavaScript way, but it is the exact same process:

Now how all that code is written is dependent on what web server you are using, but the process will always be, a client call comes in to a port, the server takes that data, gives it to your chosen web server "processor" which does the crunching and gives the server back a bunch of bytes (HTML containing data, pictures, JavaScript, etc, etc) and the server then gives it back to the clients browser.

Now I work with flask a lot, so I know how to further explain that to you, which also uses python, but it is not what you are doing, so there is no point for me to go any further because it will not apply to you. But even flask still runs on a web server piece of software that does what? It gets a request at an address with some optional data, gives it to the processor to figure out how to answer and what to answer, then take the results of that crunching and send back down the the clients browser to to the displaying.

memjr
Posts: 3651
Joined: Fri Aug 21, 2020 5:59 pm

Re: Web page with python and script

Mon Dec 04, 2023 4:53 am

neilgl wrote:
Sun Dec 03, 2023 10:34 pm
Someone remind me which webserver is actually running here - Apache2 / nginx / flask / other ?
Op.never said what's being used.

Note, that flask does come with a webserver, but flask itself is a framework only. It still requires a webserver to run in. And that could've anything, nginx, apache, etc.

khun
Posts: 13
Joined: Mon Oct 24, 2016 9:03 pm

Re: Web page with python and script

Mon Dec 04, 2023 8:45 pm

I was running with Apache2 and cgi (I think). But now I'm going to follow the recommendation to try Flask. New questions in another topic.

User avatar
neilgl
Posts: 7511
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Web page with python and script

Tue Dec 05, 2023 9:06 am

OK I have a working example for Apache2 using CGI to run python scripts when the user clicks a button on the web page.

Return to “Python”