Am trying to extract all the data from these charts found here: https://www.eurocontrol.int/Economics/DailyTrafficVariation-States.html
I am trying to get as much data from here as possible but not sure where it is in the page elements/network. I think usually the interactive charts data are stored in .json or .csv but I am not seeing any of that here.
I want to be able to get the data that appears when one hovers over the gray bars in the first table where it says "% vs 2019(daily): __ ".
Also any ways to do this through python is much appreciated!
It might be located inside this link:
https://www.google-analytics.com/analytics.js
or this link: https://www.google-analytics.com/analytics.js ,
It also might be stored on a server
Other than that I am not too sure.
I'm trying to embed an interactive plotly (or bokeh) plot into excel.
To do this I've tried the following three things:
embed a Microsoft Web Browser UserForm into excel, following:
How do I embed a browser in an Excel VBA form?
This works and enables both online and offline html to be loaded
creating a plotly html
'''
import plotly
import plotly.graph_objects as go
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [i**2 for i in x]
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=x, mode='markers', name="y=x", marker=dict(color='royalblue', size=8)))
fig.add_trace(go.Scatter(x=x, y=y, name="y=x^2", line=dict(width=3)))
plotly.offline.plot(fig, filename='C:/Users/.../pythonProject/test1.html')
repointing the webbrowser object in excel using .Navigate to the local plotly.html. Banner pops up with
".... restricted this file from showing active content that could access your computer"
clicking on the banner, I run into this error:
The same HTML can be opened in a browser.
Is there any way to show interactive plots in excel?
Finally, I have managed to bring the interactive plot to excel after a discussion from Microsoft QnA and Web Browser Control & Specifying the IE Version
To insert a Microsoft webpage to excel you have to change the compatibility Flag in the registry editor
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Wow6432Node\Microsoft\Office\16.0\Common\COM Compatibility{8856F961-340A-11D0-A96B-00C04FD705A2}
Change the DWord 0 instead of 400
Now you can insert the web browser object to excel, Step by step details are here
Edit the HTML File generated from plotly manually by adding a tag for Using the X-UA-Compatible HTML Meta Tag
Originally generated HTML file from plotly looks like this
<html>
<head><meta charset="utf-8" /></head>
<body>
Modified HTML with browser compatibility
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
After this, I can able to view the interactive plot in excel also able to do the interactions same as a web browser
Macro used:
Sub Button4_Click()
ActiveSheet.WebBrowser1.Navigate "file:///C:/Users/vignesh.rajendran/Desktop/test5.html"
End Sub
As mentioned by #jerlich, Excel blocks javascript. You should try the workaround they linked if you want full interactivity.
If you want at least some degree of controllability or interactivity, try using xlwings. With excel buttons, you can still have some communication between Excel and Python (including reading data and sending graphs).
The limitations are:
Only being able to use data entries and buttons, instead of the default plotly interactive features. Many could be replicated, but it would be more work.
It will only work on a computer you can set up your python script on (however, it looks like xlwings pro allows you to store your program inside the file)
It seems you will need to pass plotly graphs by saving and then adding the figure, because directly passing them requires xlwings pro. Direct passing without pro is possible with MatPlotLib.
Plotly guide to making interactive(ish) graphs
xlwings docs on MatPlotLib and Plotly graphs
Interactive plots require javascript to work. Excel, for security reasons, blocks that javascript. You can put a static image easily into excel.
The challenge of including javascript in excel has been addressed in this question: How can I use JavaScript within an Excel macro?
I like your question! And I'd wish I could give you a better answer, but It seems that the only way you can achieve anything remotely resembling an interactive plotly plot would be to use pyxll and follow the steps outlined under Charts and plotting / Plotly including a plot function like this:
from pyxll import xl_func, plot
import plotly.express as px
#xl_func
def plotly_plot():
# Get some sample data from plotly.express
df = px.data.gapminder()
# Create a scatter plot figure
fig = px.scatter(df.query("year==2007"),
x="gdpPercap", y="lifeExp",
size="pop", color="continent",
log_x=True, size_max=60)
# Show the figure in Excel using pyxll.plot
plot(fig)
This will produce the following plot:
Alas, this will not be a fully interactive plotly plot like we all know and love, since it's also stated on the very same page that:
The plot that you see in Excel is exported as an image so any
interactive elements will not be available. To make a semi-interactive
plot you can add arguments to your function to control how the plot is
done and when those arguments are changed the plot will be redrawn.
But as far as I know this is as close as you'll get to achieving what you're seeking in your question. If you're not limited to Excel, but somehow limited to the realm of Microsoft, one of the commenters mentioned that you can unleash a fully interactive plotly plot in PowerBI. If that is an option, you should take a closer look at Is it possible to use R Plotly library in R Script Visual of Power BI?. This approach uses R though...
I have an HTML template and I would like to insert plotly figures in specific places in the HTML file. I have three figures and use the following code to add the figures to the HTML file:
figures= [fig1, fig2, fig3]
with open('test.html', 'a') as f:
for fig in figures:
f.write(fig.to_html(full_html=True))
When I do that it adds all three figures at the bottom of the file, after the rest of the code in the template. Does anyone know how I can add each of the three figures after the appropriate headers? I think I read somewhere that I can do this with BeautifulSoup?
import chart_studio.tools as tls
tls.get_embed('https://plotly.com/~chris/1638')
Based on the documentation [link] https://plotly.com/python/embedding-plotly-graphs-in-HTML/#:~:text=Plotly%20graphs%20can%20be%20embedded,Plotly%20graphs%20with%20the%20plotly. the above code should do it for you. There is a lot of examples at this link as well.[link]https://plotly.com/python/interactive-html-export/
I have an interactive graph generated by Plotly in Python that I saved to an html file using plotly.offline.plot(fig, filename='/tmp/interactiveGraph.html')
I am now trying to embed this interactive graph into some kind of webpage, using either Dash or Django. I'm leaning toward Django at the moment, given that I have an html version of the graph. Which would be better?
My code for the webpage is in a separate file from the code where I am creating the graph.
A lot of the tutorials I've found online just say to add a few lines to the template, but I don't know how to get those lines that they've described.
tl;dr: I'm looking for guidance as how to integrate an html file-for a Plotly interactive graph-with a web python script using Django or Dash
Side Question:
what is the difference between
plotly.offline.plot(fig, include_plotlyjs=False, output_type='div')
and what I have above?
Reference:
https://github.com/ricleal/DjangoPlotLy
https://www.pythonsetup.com/how-implement-data-visualization-django-and-plotly/
I would highly reccomend Django, its a great framework. As for this, the best option is to generate the data via JavaScript and Plotly has a great library for this. If you must use python, then Django can be used. Assuming you are familiar with Django, inside of your view you can collect your data and build your graph ( I would reccomend a celery task for something long running like this unless they are small graphs), and you can then collect the return from creating the graph in div format. This will return a string that holds all the needed html and css for the graphs. Put this inside of you get_context_data() method in your view and place it into the dictionary. You can the use that object inside of a template. I have done this before, if you are having a hard time feel free to DM me. Hope this helps some!
In regards to your side question, I believe having False for including JS will make the graph a bit smaller assuming you have an include for the plotly JS library. They might have done this in a newer release to make the graphs faster as they were significantly slower in the browser from python that the JS rendered one.
I am using bokeh to plot my math functions created with python/numpy.
I would like to use sliders as shown in
http://docs.bokeh.org/en/latest/docs/server_gallery/sliders_server.html
Once I create the html file with the plot, I would like to select different values on the sliders which modify the plot and then read back the chosen values in into python to use it for other manipulations.
What is the best way to read the chosen value on the slider from the html file back into python ?
I saw pyquery could be useful, but I cannot really figure that out.
Any suggestions would be appreciated based on above scenario.
There are two slider examples in the bokeh repo, where the slider is connected back to python via the bokeh server.
Sliders App
Taylor server
If this isn't what you were after, can you elaborate a little more?
A static HTML file and the state of the slider is inside a web browser and never reflected back to the HTML file. What you should be doing is to use bokeh-server - answered via bokeh-google group