Display Sensordata in webpage - python

I am retrieving temperature data from a sensor continuously. Now I want to display them in a webpage hosted by a node.js webserver. I struggle to understand how these data are beeing send to my html webpage because there are many ways doing that without making any way clear for me. I read in this context terms like REST, AJAX, POST and GET.
Can someone make it clear for me which would be the easiest choice in that case.

All those terms are connected with one another:
REST is a software architecture used for creating web-services that allows a requesting system (e.g. your browser) to access and/or manipulate data on the server.
GET and POST are two HTTP methods that define what you want to do to the data on the server (get it, change it, add something, ...).
Ajax is used on the client-side to retrieve data from RESTfull services.
In your case, you would create a GET endpoint in node.js (with e.g. express) and then connect to this endpoint via Ajax to retrieve the data and display it to your website.

Related

How to fetch Python data with Angular

maybe it is simple but I didn't find a satisfactory answer yet.
I have a python application that collects data over CAN bus (temperature, weight, ...) and I want to visualize them over Angular.
On the one side, I wrote the Python application that cyclic read the CAN-bus data and writes them to the console and on the other hand I wrote a small Angular application that contains the first step a simple table.
Now I want to fill in the table every 10 seconds with data from the Python application instead of printing them to the console.
How can I connect these both?
My first thought was a simple file where I save the values from Python and read them with Angular.
The second solution is a database, but I think this is too much for only a few values
So is there a direct way to access the Python data from Angular?
Basic idea is to create an api in python and let angular consume that
then there is the question of weather you want to have backup data in python,
if so then save it a db or file and use that as response for angular
if you want to do some fancy real time stuff may be look into long polling or http event stream
There are several ways you can access Python data from an Angular application:
One way is to use a REST API. You can create a REST API in Python
using a web framework like Flask or Django, and then use Angular's
HTTP client to make requests to the API and retrieve the data.
Another option is to use WebSockets. You can use a Python library
like asyncio or websockets to set up a WebSocket server, and then
use Angular's WebSocket client to connect to the server and receive
updates in real-time.
You can also use a message queue like RabbitMQ or ZeroMQ to allow
your Python and Angular applications to communicate with each other
asynchronously.
Overall, the best approach will depend on your specific requirements and how you want to structure your application. A REST API is a good choice if you need to retrieve data from the Python application on demand, while WebSockets or a message queue can be used for real-time communication and updates.

What is the most efficient way to reload a div without refreshing the whole page in django?

I am looking for the most efficient way that I can present on a live web page data such as a table with data from the database, that when a new record is added to the database the table updates itself without the user having to refresh the page.
I have tried the method with Django and Ajax but I don't find it efficient because it has to refresh at a certain time but I only want it to refresh when a new record is added to the database.
What methods could you recommend?
With standard Ajax, the client has to initiate the request. If you want the server to be able to "push" to the client without a request from the client, you'll want to use websockets. There's a package called Django Channels for implementing websockets in Django.

Backend script passing data to client-side script

Say that I have a Python Flask server running which has a backend script that produces a number and a string.
How can I pass the number and string from the backend to a script that runs of client-side so that the user can run it rather than the server?
Example:
backend script data_producer.py produces "asdaslkdjasdlksja" and 18 from its functions
I want to pass "asdaslkdjasdlksja" and 18 to a Brython or JavaScript HTML embed ( tag) so that it can be processed in the browser and the results be sent back to my server.
Edit: I realized that I can just use Jinja2's "{{ }}" when rendering a template so that I can use data in an HTML script embed
The question you asked is too broad. It's almost equivalent to asking, how you can connect two computers. Since you haven't even specified any data type, the first thing that comes to my mind is using sockets which is as low level as you can get.
A more high level and appropriate approach would be to use an HTTP REST server (with flask-RESTful), since you already have a flask server running.
However, there's another million ways to transfer data between two Python scripts, from WebSockets, WebRTCs, sshing, the new IPFS to even emails. Now most of them would probably be overkill, so I would suggest you to make a simple REST server and make the client send GET or POST requests to it.
After looking at the new edit, I still think a REST api is the best option. Since you can easily make a GET or POST request using the fetch api in Javascript. In Brython you can use ajax to do the same thing.

Is there a way to maintain a session through links in python?

I am creating a project in python that scrubs websites and sends links to customers. I currently have functioning versions that simply relay information, but it would be much better if I could make it easier by sending post requests to the server. For example, I currently send a link to my users to a certain product. I want to know if, instead, I can perform post requests in my program and send them a link that is part of the same session where that POST has been done already (apologies for poor use of terms). Basically completing actions for them and sending it through a link?

invoke python script and populate results on webpage

I created a simple python script which takes a URL as input and once passed will do curl using multiple proxies and show the response code, now I want to create a webpage where others can use(my colleagues) as it will help them too, I want to create simple webpage which let them select set of proxy addresses, and input URL and upon submission, it will run the script on a machine(webserver) and populate the result to webpage using dynatable or datatable frameworks, but am not sure how or if it is possible as I didn't worked much in webserver thing, I want to know what tools I will need and how do I design it.
If python script can be called in terminal(as it needs to run curl) and show result on webpage based on output from script(which I will export to csv file), how can I do that? what to use xampp, wamp, lamp etc ?
You need a framework for this, something that will listen to your request coming from the front-end (webpage), there are tons out there as python framework, you can check bottle framework as a starting point.
So the flow would be something below.
1. from webpage, a request is sent to the backend
2. backend receive the request and run your logic (connecting to some server, computing logic, etc)
3. once backend process is done, backend then send the response to webpage
you can either use a REST approach or use templating functionality of the framework
You will need a request. You can do this in JavaScript with an ajax request there are frameworks to hook it up straight to your python which allow you not to code the JavaScript https://www.w3schools.com/xml/ajax_intro.asp there are many JavaScript frameworks that will make this easier/ shorter to code.

Categories