HTML - python bidirectional communication - python

I have the following problem:
The big picture: steering a stepper motor via broswer. The stepper motor is connected to a Fox Board with debian. On the board an apache webserver is running.
I have a HTML website with a form, in which the user enters the parameters for the motor. The submit button starts a python script, which does the communication with the motor. The motor delivers some data to the python script. The script sends this information back to the HTML page in an iframe.
web page with form -->
parameters input by user -->
submit -->
python script is started -->
motor does something -->
motor sends answer to python script -->
answer is submited by python to web page in iframe.
Works...
One value that is returned by the motor is important, if it exists, I would like to have it in my form in the web page. Not only displayed in an iframe.
My question is:
a) Is this possible in principle?
b) if yes, how can it be done? Meaning, which are key words to search for? (No clue right now)
c) Is there an alternative.
To a: A self invoking php script could be a solution. But how can I program such a script, that while pushing a submit button, the php is self invoking, the python script gets started, the php waits until python delivers feedbach and finally, the php builds up itself with the data recieved.
I'm on the edge of my own incompetence...
I appreciate any help, suggestions etc... Please just send me in the right direction, Peter

You could do it with AJAX (asynchronous javascript) and python web services, in general.
The communication would be handled on the client with javascript, and it would invoke your python script (the web service), which would do the work with the motor, and then send the response back to the web page, which would be able to show new readings/state (again, through javascript).

Related

Can I connect a Python GUI to a website using HTTP?

Is there a way to connect to a website from a Python Tkinter GUI using HTTP requests? Essentially, I want the following functionality:
Press a button on the GUI
A signal is sent to a website (from the GUI) that this button was pressed
Send information along with that signal
I only need to focus on the GUI side of this. I have no code to go along with this - I was just wondering if it's even possible.
There are three components to what you ask. Your GUI can send information to your web server using some URL you would have to invent. It can be as simple as:
import requests
requests.get("http://example.com/information?name=Joe+Smith")
Then, your web server needs to respond to that request by saving the information somewhere. Your web server also needs a similar request to return the information. Then, your web page needs Javascript on a timer doing an AJAX request to fetch that info, and to change some field on the page in response. That depends on what Javascript tools you're using.

Run a python program from browser

I want to run a python program (kinda like this) from a browser
Anyway, as you see it has a few inputs, and i would like to "translate" that into a kind of form
def addNew():
appendBase = open('dBase.cfg','a')
uname = input('Username: ')
pword = input('Password: ')
appendBase.write(uname+','+pword+'\n')
print('\nAdded new profile: \n'+uname+','+pword)
appendBase.close()
Also i dont know how to get the print to the page, so it can show it
I've just started learning, so go easy on me, please
It is not possible to actually run this in the browser, for various reasons.
you can't run python in browsers. only javascript
you can't open local files from a browser
there's no command line to input from some terminal
Most things you see on the web have two parts
a part that actually runs in the browser. Written in HTML and javascript
another part where the browser connects, to send and receive data. That can be done in any language, including python. However, that part is not visible in the browser
The two parts communicate using HTTP protocol. So, start by reading a bit on HTML/javascript (W3Schools is an easy way to get started). When you feel comfortable with that, practice with a python web framework (django is the most popular, but flask is the easiest to get started), and see how javascript uses HTTP to connect to that.

realtime listening GET parameter on python

I had a pi-copter project. My idea to control it is using web page. I have done it using php. But the copter running gyro sensor to makes it stable while flying. So, to run and get data from the sensor, it have to be programmed. I using python to run the sensor. the script will process a control to esc from orientation and from data that I sends using front-end web page.
my question is how python listens GET data from front-end web page?

Pushing updates from Python server to a web interface

I've written an algorithm in python and a web interface around that. After you submit the form and start the algorithm, I'd like to push and update data on the page as it's running. How can I accomplish this?
To have real-time or semi-real time communications between the web page the options are
Automatically refresh the page after certain seconds using meta refresh tag in HTML <head>
Fetch updated data with JavaScript and AJAX HTTP GET: https://api.jquery.com/jquery.get/
Use server-sent sent events: http://www.html5rocks.com/en/tutorials/eventsource/basics/
Use WebSockets: http://www.html5rocks.com/en/tutorials/websockets/basics/
All approaches, excluding the first one, require rudimentary JavaScript skills besides knowing server-side Python. The latter two approaches recommend advanced understanding of real-time communications. Thus, if you are not familiar with the web development I recommend picking the meta refresh tag.
On the server side you need to start a process or a thread which to handle the long running process, then have this process to write its progress to a database. When the web UI updates itself, it reads the latest results from the database and pushes/pulles them back to the browser.

Python long running process

I have a Python web application in which one function that can take up to 30 seconds to complete.
I have been kicking off the process with a cURL request (inc. parameters) from PHP but I don't want the user staring at a blank screen the whole time the Python function is working.
Is there a way to have it process the data 'in the background', e.g. close the http socket and allow the user to do other things while it continues to process the data?
Thank you.
You should use an asynchronous data approach to transfer data from a PHP script - or directly from the Python script, to an already rendered HTML page on the user side.
Check a javascript framework for the way that is easier for you to do that (for example, jquery). Then return an html page minus results to the user, with the javascript code to show a "calculating" animation, and fetch the reslts, in xml or json from the proper URL when they are done.

Categories