-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display multiple mpld3 exports on a single HTML page #128
Comments
Hi,
Let me know if that makes sense, and please let me know if you think there might be a better way to approach this application! |
Thanks Jake. That makes sense. I'm not well versed in JS and so this question might seem simplistic but here goes... Would both calls to `mpld3.draw_figure()' sit within the same script tags, and just be called one after the next? This approach makes sense and I've done similar using the Vincent library to output Vega-friendly JSON. Perhaps just an example code snippet in the docs might highlight the use case for newcomers. Once I get this running I'll find a moment to submit an update to the docs. |
For good measure, I just answered the question on stack overflow as well, and included a code snippet: http://stackoverflow.com/questions/22981359/display-multiple-mpld3-exports-on-a-single-html-page/ Let me know if that helps! |
Thanks for the replies Jake, much appreciated (also btw started watching the Pycon scikit-learn talk but need to set time aside for the full session - nice work). I've followed the steps and it's still not rendering the plot. Comparing my HTML output vs the generated HTML snippet I am quite sure it's to do with loading the Here's my output html from the
And below are the segments of flask code and template to generate: CODE, excluding imports. I'm aware that some of this code (eg the random generator) is a bit of a hack to begin with but I'm a) a relative beginner and b) just trying to get a proof of concept up before tidying up:
Template: <html>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript" src="http://mpld3.github.io/js/mpld3.v0.1.js"></script>
<head>
</head>
<body>
{% for plot in dict_of_plots %}
<div id="{{ plot['id'] }}"></div>
{% endfor %}
<script type="text/javascript">
{% for plot in dict_of_plots %}
mpld3.draw_figure("{{ plot['id'] }}", {{ plot['json']|safe }});
{% endfor %}
</script>
</body>
</html> |
I think the issue is that valid div ids cannot start with a digit. |
I believe that's a requirement of HTML, by the way, not of d3/mpld3. |
That's it! I added a quick |
Comments on the docs (or even better, pull requests 😄) would be really helpful! Thanks |
Hello there ! |
No, you don't need flask. All mpld3 does is produce JSON or html. Flask is a separate tool that lets you embed this output within a webpage or an app. If you'd like to simply generate html, you can run something like the following: import matplotlib.pyplot as plt
import mpld3
fig, ax = plt.subplots()
ax.plot(range(10))
mpld3.save_html(fig, "my_plot.html") Opening the resulting html file with a browser should show you the plot. Regarding multiple plots on a page, this stack overflow answer should help: http://stackoverflow.com/questions/22981359/display-multiple-mpld3-exports-on-a-single-html-page |
Exciting, I am going to try this Thanks for doing this @jakevdp |
@jakevdp I apologise for my ignorance and/or inability to find how to do this but how would I embed a json output of mpld3, using:
into an html slide deck? For clarity, I would rather not clip the json in to the html directly (as I want to be able to rerun my python script to obtain the plots). I'm guessing I'm just missing some basic json/html knowledge... |
Yes, you'd have to write a one-liner to load the JSON into javascript, and then call A google search turned-up this: http://stackoverflow.com/questions/7346563/loading-local-json-file |
I've been trying that (I really don't know what I'm doing here). So would
Are there any libraries or js I need to put somewhere? (currently have Thank you very much for your response! :) On Thu, 5 Feb 2015 19:43 Jake Vanderplas notifications@github.com wrote:
|
I would use
And here is a notebook that puts it together, which you will have to download and open with your own IPython because nbviewer is too paranoid to display it. |
Thanks Abraham, I'll try that out! Vince On Fri Feb 06 2015 at 8:31:54 AM Abraham Flaxman notifications@github.com
|
I am trying to display multiple htmltooltip plugin plots in a page. The plots shows up. But the HTMLtooltip plugin doesnt work. |
I've been trying to work out how to export multiple individual figures to display them in a template using Flask. I think the solution is somewhere between the
.fig_to_html
and.fig_to_dict
methods but I'm not JS-savvy enough to work it through.There's a fuller description on this stackexchange question:
http://stackoverflow.com/questions/22981359/display-multiple-mpld3-exports-on-a-single-html-page
I'd be happy to submit an pull request to the docs if someone can explain what's needed.
The text was updated successfully, but these errors were encountered: