I have a python script that collects data on some virtual machines. I want to display this data in a webpage.The web page must be dynamic since the script will run continuously and the data must be updated every time the script runs. I possibly want this data displayed in a table but I am not sure what direction to go?
You could use Apache or another web server. Set up an HTML web page with javascript. I recommend using jQuery as it makes making ajax calls so much easier. Have your javascript/ajax/jquery call your python script every x minutes/seconds.
Ensure your apache server is setup to run CGI scripts and ensure they are set with read and execute permissions.
Related
I am using Python 3.6.1 with Flask, with the goal being to load a webpage, display live loading information via websockets, then once the loading is complete redirect or otherwise allow the user to do something.
I have the first two steps working- I can load the page fine, and the websocket interface (running on a separate thread using SocketIO) updates properly with the needed data. But how can I make something happen once the functions that need to load are finished? To my understanding once I return a webpage in Flask it is simply static, and there's no easy way to change it.
Specific code examples aren't necessary, I'm just looking for ideas or resources. Thanks.
I'm working on a platform that uses Django and Django REST Framework, so this platform is used to monitor the state of some sensors which send data to the computer.
Now I have the data in the computer, and I want to create a Python file to read the data from the computer and store it in a MySQL database that Django will read and show to the user.
I have two questions:
Can I make Django read from the file, save the data in the database, and show it in the user interface?
If not, how could I make a script to run in the background when I start the project?
Can i make django read from the file save the data in the database
Yes, you could create a management command for this which could be run from crontab. Or create a sort of 'daemonized' version which keep on running but sleeps for X amount of seconds before running again. This command reads the data, puts it into the database.
show it in the user interface ?
Yes, but I would advise against doing this sequential (as in, don't put your 'data reading' in your view!!). So your managment command updates the database with the latest data and you view only shows the latest data.
You could also use this
https://github.com/kraiz/django-crontab
That makes it even more simple to use
I'll briefly explain what I'm trying to achieve: We have a lot of servers behind ipvsadm VIPs (LVS load balancing) and we regularly move servers in/out of VIPs manually. To reduce risk (junior ops make mistakes...) I'd like to abstract it to a web interface.
I have a Python daemon which repeatedly runs "ipvsadm -l" to get a list of servers and statistics, then creates JSON from this output. What I'd now like to do is server this JSON, and have a web interface that can pass commands. For example, selecting a server in a web UI and pressing remove triggers an ipvsadm -d <server>... command. I'd also like the web UI to update every 10 seconds or so with the statistics from the list command.
My current Python daemon just outputs to a file. Should I somehow have this daemon also be a web server and serve its file and accept POST requests with command identifiers/arguments? Or a second daemon for the web UI? My only front end experience is with basic Bootstrap and jQuery usually backed by Laravel, so I'm not sure if there's a better way of doing this with sockets and some fancy JS modern-ism.
If there is a more appropriate place for this post, please move it if possible or let me know where to re-post.
You don't need fancy js application. To take the path of least resistance, I would create some extra application - if you like python, I recommend flask for this job. If you prefer php, then how about slim?
In your web application, if you want to make it fast and easy, you can even implement ajax mechanism fetching results based on interval to refresh servers' data every 10 seconds. You will fetch it from json served by independent, already existing deamon.
Running commands clicked on Web UI can be done by your web application.
Your web application is something extra and I find it nice to be separated from deamon which fetch data about servers and save it as json. Anytime you can turn off the page, but all statistics will be still fetching and available for console users in json format.
I have written a scraper tool in Python which when executed produces a CSV file of information. I wish to embed it in a HTML, so within the page the user is able to run it and then the results are displayed on the page from the CSV file. How can I do this?
If you only need to display the information on a website, no interaction back with your python script, all you need to do is just write the results into a HTML file and .format() it appropriately.
However, since this requires the user to be able to run and view the results with interaction with your python script, you would need a web server.
You could try the two most popular python based web servers that can accomplish this: Flask and Django or you could use the less common but more lightweight Simple HTTP Server.
But, do keep in mind that an easier alternative (if possible) will be to just use a GUI such as Tkinter or PyQt or EasyGUI.
Is it possible for my python web app to provide an option the for user to automatically send jobs to the locally connected printer? Or will the user always have to use the browser to manually print out everything.
If your Python webapp is running inside a browser on the client machine, I don't see any other way than manually for the user.
Some workarounds you might want to investigate:
if you web app is installed on the client machine, you will be able to connect directly to the printer, as you have access to the underlying OS system.
you could potentially create a plugin that can be installed on the browser that does this for him, but I have no clue as how this works technically.
what is it that you want to print ? You could generate a pdf that contains everything that the user needs to print, in one go ?
You can serve to the user's browser a webpage that includes the necessary Javascript code to perform the printing if the user clicks to request it, as shown for example here (a pretty dated article, but the key idea of using Javascript to call window.print has not changed, and the article has some useful suggestions, e.g. on making a printer-friendly page; you can locate lots of other articles mentioning window.print with a web search, if you wish).
Calling window.print (from the Javascript part of the page that your Python server-side code will serve) will actually (in all browsers/OSs I know) bring up a print dialog, so the user gets system-appropriate options (picking a printer if he has several, maybe saving as PDF instead of doing an actual print if his system supports that, etc, etc).