scottyaguas
Posts: 2
Joined: Tue Feb 05, 2019 6:00 pm

Update variables in python script from web input

Tue Feb 05, 2019 8:09 pm

Hello,

I would like to set my pi up as a server and be able to update the values of variables in my python script with values coming from an input on the web page. For example, I want a stepper motor to make a certain number of steps based on value inputted into a box on a web page. Is it possible to do this? I know there are ways to control the GPIO from a web page but that is not what I want to do. I want my python code to control the GPIO and just have the input from the web page update the value of a variable in the code.

I have followed a tutorial on how to set up a server with Flask, I think this might be a good start. If anyone has done something like this or knows how to do this, please let me know.

Thank you

Andyroo

Re: Update variables in python script from web input

Tue Feb 05, 2019 10:21 pm

If you are just after numbers being input then step 4 of this will give you a starting point https://blog.pythonanywhere.com/169/ or (if you can stand the ads) https://www.journaldev.com/15524/python-flask-tutorial

If you want to get fancy (why not run before crawling) then this may help https://www.instructables.com/id/Smooth ... r-Control/

Its a bit of a mix of Python, Javascript and a specific control board but if you pick it apart you can find:

1) The idea of using a html page to have the sliders on
2) Displaying this with the Python FLASK module
3) Reading the response back into Python

bzt
Posts: 564
Joined: Sat Oct 14, 2017 9:57 pm

Re: Update variables in python script from web input

Wed Feb 06, 2019 10:10 am

Hi,
scottyaguas wrote:update the values of variables in my python script with values coming from an input on the web page
This is more problematic than you think. Keep in mind how web cgi works:

1. your browser asks for an URL
2. the webserver receives the request and sees that's a cgi
3. the webserver calls a python code (cgi script), passing the form input values in environment
4. the python code generates the output
5. the python code EXISTS
6. the webserver sends back the script's output to your browser

Every time you pass an input value from a browser, the webserver starts a NEW script. So this means that variables only live as long as the return html is generated, but no longer, and their values are initialized every time. If you want a permanent solution, where you can keep the value in memory indefinitely, I'd suggest to run a python script in the background (which does not exit after page generation therefore keeps the values of variables), and pass the web input to that (via files, pipe, database etc.) from the cgi script. Alternatively use shared memory from your cgi script (SHM, mmap() etc.) which keeps variable values across script restarts.

Cheers,
bzt

pfletch101
Posts: 624
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Update variables in python script from web input

Wed Feb 06, 2019 3:01 pm

The way I generally handle this sort of problem is to have the user interface code write the data which is to be used in the (motor) control code to a file in a known location on the server - usually in CSV format - and then have the motor control code read the data from the file. Any of the applications and/or languages that you are likely to be using will be able to read and write text files, and the text file does not depend on either or both of them to be running all the time for its continuing existence. Files are also a useful way of sharing data on a web page between JavaScript code and the server-based scripting language of your choice. [Edited to correct confusing typo]
Last edited by pfletch101 on Wed Feb 06, 2019 6:50 pm, edited 1 time in total.

Andyroo

Re: Update variables in python script from web input

Wed Feb 06, 2019 6:25 pm

If you read the op at the end FLASK has been loaded so Python is actually the web server and handling all requests so you do not need cgi etc and variables stay within the Python scope :D

It’s great if you have a low traffic site that needs complex programming

Return to “Networking and servers”