Skip to content Skip to sidebar Skip to footer

How To Output A Graph From Matplotlib In Django Templates?

Given this example taken from here: def simple(request): import random from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figu

Solution 1:

When I had a similar situation, I used two views. One used matplotilb to generate a PNG, while the other used used django templates to create an HTML page that presented the PNG (and some other data). The param sent to the template was just the PNG filename. The other view is then attached to he appropriate .png URLs.

One problem is if you want to calculate some parameters which are used both for generating the HTML and PNG. I encoded such information in the filename. This is painful, and slightly hacky, but it's s also good for the user if all the info is in the filename when she saves it.

Post a Comment for "How To Output A Graph From Matplotlib In Django Templates?"