Home > OS >  How to fetch Python data with Angular
How to fetch Python data with Angular

Time:12-18

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?

CodePudding user response:

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

CodePudding user response:

There are several ways you can access Python data from an Angular application:

  1. 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.

  2. 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.

  3. 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.

  • Related