Highcharts SVG Export from Python Server Side Code - python

I am using reportlab in python to render a pdf server side.
I really like the look of highcharts graphs. But I am building a pdf server side which needs to include some graphs. The server side graphing (reportlab and matplotlib) do not have nearly as much choices for formatting / design.
Is there a way I can run a client side javascript graphing solution (highcharts) from the server side?
Here is a post with that request and this is what they said:
You feed this Java tool a URL, it fires up its internal WebKit browser, executes the JavaScript code and dumps the rendered page to PDF.
I am looking for resources on how this could be accomplished in python.

I had similar requirements and couldn't find an existing solution so I created a little proof-of-concept solution here.
This implementation works by having the python process create a node subprocess that uses the highchart-export-server node module which in turn spawns a PhantomJS instance to render the chart. A few russian dolls involved there, but it does the job.

Open a web browser on the server.
Point the web browser at a pre-made page that loads highcharts and renders your chart.
Grab the output of the browser.
Send that to a client.
Which of those steps seems confusing?
Note that this will be significantly slower than just using matplotlib, because you need to start up a full web browser just to render a graph. I had a quick look at the highcharts page you linked and I think you could probably reproduce that in matplotlib.

Related

Is there a way to embed pandas analysis into a html page?

I've done some analysis using Pandas on a csv file and have created a number of graphs using matplotlib and plotly.
I'm trying to get these graphs in a html page so my team can see them on a dashboard.
I know it's possible to download the image and embed into html, however, the csv file I performed my analysis on will be constantly updated i.e. I need a way of include the graphs on a html page which auto refreshes.
Is this possible?
I think it would be possible and check pyscrpit
https://pyscript.net/
where you can use python in html
https://github.com/pyscript/pyscript/blob/main/docs/tutorials/getting-started.md
here are some tutorials to get and idea of what I am talking about and to start
Then you can host the html page anywhere you like and they will see when you update it
Voila or Panel to make a dashboard is another way to go. That needs an active Python kernel at this time. (That is as opposed to the pyscript / webassembly route. JupyterLite currently works in WASM and Voila in WASM is in development.) Examining demonstrations available at the Voila Gallery will give you a sense of how it can work. All those example demos are served via MyBinder. And you can use that too, if your code and data can be public. If it cannot be public, you'd need to host a server that only your team can access.
JupyterLite would also be possible if you don't mind your team being able to see the code cells and things that compose your Jupyter notebook stepping through the analysis. That would not require an active Python kernel server-side because it is based on WebAssembly that would run inside the client machine, like PyScript. So all you would need to do was a have a server that can serve static files. That's a lower bar than Voila that would run on the server. How to get started deploying JupyterLite is here.
I would checkout out Dash or Plotly, here you can add generation of the graphs in a callback for isntace. Which would update them when the .csv updates. Good luck!

How to create an HTML page that refreshes in real time using Python?

This is a simple data visualization task that I am trying to accomplish: seeing data in real time using my web browser.
My idea is to take an HTML template and change only a few key values inside of it once a second using Python. I need it to be HTML and not something else like pygtk because CSS formatting is very practical for my application.
Is that possible? What would be the easiest way?
the Flask module should solve your problems
Use javascript setInterval and XHR.
See https://www.w3schools.com/jsref/met_win_setinterval.asp
https://www.w3schools.com/xml/xml_http.asp
https://makitweb.com/how-to-fire-ajax-request-on-regular-interval/
AJAX updates part of webpage in realtime without refreshing the whole page.
using Ajax in Django is to use the Django Ajax framework.
The most commonly used is django-dajax which is a powerful tool to easily and super-quickly develop asynchronous presentation logic in web applications, using Python and almost no JavaScript source code. It supports four of the most popular Ajax frameworks: Prototype, jQuery, Dojo and MooTools.
https://www.tutorialspoint.com/django/django_ajax.htm#:~:text=Using%20Ajax%20in%20Django%20can,library%20like%20JQuery%20or%20others.&text=The%20most%20commonly%20used%20is,almost%20no%20JavaScript%20source%20code.

Web App to read data continously from another app

I have a python script that continuously generates JSON object
{ time: "timestamp", behaviour: 'random behaviour'} and display it on stdout.
Now I want to build a web app and modify the script so that I can read continous data to my web page. I donno where to start from.
Well in details: I want a web API to have an on/off button and by clicking on it start reading the beahviour and on clicking off it stops.
I am very new to programming so please suggest me topics and ways I need to study/ look upon to make this app.
thanks
Here's a simple way I'd go about doing it:
Create a backend service that exposes the python script's output via HTTP. You can use any familiar backend programming language: python, java, .net etc to host a simple http web server, and expose the data.
Create a simple html page, that makes an ajax call to your backend service, pulls the data, and shows it on the page.
Later add your buttons, make the page nicer, responsive on other devices.
Topics to read depends on what you are missing above. My best guess based on your question is that you are not familar with front end programming. So read up on html, js, css and ajax.

Going to need to frequently update a graph in a web application, what technology should I use?

I've got some code in Python that I'd like to start trying to turn into a web app (not sure what framework I'll use yet, but it will be python-based). The idea is that the user punches in a bunch of numbers, and a graph is updated after each is entered.
Currently I'm doing this with python + matplotlib. My initial thought was to re-render a jpg of the image each time the user punches in a new number, but this seems a little heavy.
I'm wondering, what is a technology I should look at for incorporating a dynamic graph into my web application?
regenerating a jpeg for every number would be very heavy.
Javascript provides a number of extremely high quality free graphing libraries:
http://webdesignledger.com/resources/13-useful-javascript-solutions-for-charts-and-graphs
In addition to the above link there are tons of stack overflow posts on javascript graphing libraries.
If you need to process each user input with python then i would consider using ajax. It could be like:
user inputs a number
browser/javascript makes an ajax request to your python app
python processes and returns the appropriate data
browser recieves response and updates the JS chart

How do I screen capture a URL with PIL?

I'm trying to write a server process that will allow you to enter a URL, then every 30 min ping that URL and capture it as an image. Is this possible with a combination of something like CURL, urllib2 and PIL?
Curl, urllib2, etc., grab the HTML code for a web page. But a page doesn't look like anything on its own. Instead, a browser uses that code and renders a web page according to its own internal rules of how that code should be used. And, of course, each browser renders the page slightly differently.
In other words, you can't take a snapshot of a page without having a web browser to generate the page to take the snapshot of.
If you're feeling very ambitious, you can create your own custom, scriptable page renderer by using the rendering engine from the browser of your choice -- they all make the rendering engine a separate component that you can work with separately. IE's is called "Trident", Firefox's is called "Gecko", Chrome's is "WebKit", etc.
Otherwise you'll want to just do some sort of UI scripting, like you might do with iOpus or Selenium. Selenium is scriptable with python, so that's one for you right there.
EDIT
Here you go. That looks pretty simple.
The ImageGrab can be used to take a screenshot on windows. However, you can't do this purely using CURL, urllib2 and PIL, because you will have to render the web site. The easiest would probably be to open the website in a browser and grab a screenshot.

Categories