librelist archives

« back to archive

[flask]using matplotlib to render data to webpage directly

[flask]using matplotlib to render data to webpage directly

From:
刘一新
Date:
2011-09-29 @ 08:43
Hi, everyone.
I want to plot some data on the webpage directly on the webpage without
saving the graph as a file. By google search, I found a solution here:
https://gist.github.com/862153. However, this solution directly render the
data in a single webpage. I want to render the data as part of a webpage,
which is much like a <img> tag. Then, I can add more things to that page.
What  should I do?
Thanks.

Eshin

-- 
*Dr. Yi-Xin Liu*
*Department of Macromolecular Science*
*Fudan University*
*Room 415, Yuejing Building *
*Handan Rd. 220, **Shanghai, China*
*Tel +86-021-65642863*
*Mobile +86-13916819745*
http://www.mendeley.com/profiles/yi-xin-liu/

Re: [flask]using matplotlib to render data to webpage directly

From:
Simon Sapin
Date:
2011-09-29 @ 09:10
 On Thu, 29 Sep 2011 16:43:21 +0800, 刘一新 wrote:
> Hi, everyone.
> I want to plot some data on the webpage directly on the webpage
> without saving the graph as a file. By google search, I found a
> solution here: https://gist.github.com/862153 [1]. However, this
> solution directly render the data in a single webpage. I want to
> render the data as part of a webpage, which is much like a  tag. 
> Then,
> I can add more things to that page. What  should I do?
> Thanks.
>

 Hi,

 The example you gives serves the plot as a signle PNG file at a given 
 URL. You can then include it in an HTML page with <img src="{{ that_url 
 }}>. Of course if the data is not always the same, you may want to have 
 some parameters in the URL. The image is not saved in a file, but it is 
 created in a different HTTP request than the HTML.

 If you really want to create the plot and the HTML content in the same 
 HTTP request, you can encode the PNG data in base64 and include it in 
 the HTML with a "data:" URL.
 See http://en.wikipedia.org/wiki/Data_URI_scheme (there is a Python 
 exemple there too)

 Regards,
-- 
 Simon Sapin

Re: [flask]using matplotlib to render data to webpage directly

From:
刘一新
Date:
2011-09-29 @ 09:23
Thanks, Simon

Few minutes ago, I figured out the same method as your first one by reading
a article on web2py and matplotlib (Di Pierro, M. "Web2Py for Scientific
Applications" Comput. Sci. Eng. 2011, 64.)

BTW: If the data I want to plot is changing, is there any way to refresh the
image I just rendered? I think this is similar to play a streaming vedio.

Eshin

2011/9/29 Simon Sapin <simon.sapin@exyr.org>

>  On Thu, 29 Sep 2011 16:43:21 +0800, 刘一新 wrote:
> > Hi, everyone.
> > I want to plot some data on the webpage directly on the webpage
> > without saving the graph as a file. By google search, I found a
> > solution here: https://gist.github.com/862153 [1]. However, this
> > solution directly render the data in a single webpage. I want to
> > render the data as part of a webpage, which is much like a  tag.
> > Then,
> > I can add more things to that page. What  should I do?
> > Thanks.
> >
>
>  Hi,
>
>  The example you gives serves the plot as a signle PNG file at a given
>  URL. You can then include it in an HTML page with <img src="{{ that_url
>  }}>. Of course if the data is not always the same, you may want to have
>  some parameters in the URL. The image is not saved in a file, but it is
>  created in a different HTTP request than the HTML.
>
>  If you really want to create the plot and the HTML content in the same
>  HTTP request, you can encode the PNG data in base64 and include it in
>  the HTML with a "data:" URL.
>  See http://en.wikipedia.org/wiki/Data_URI_scheme (there is a Python
>  exemple there too)
>
>  Regards,
> --
>  Simon Sapin
>



-- 
*Dr. Yi-Xin Liu*
*Department of Macromolecular Science*
*Fudan University*
*Room 415, Yuejing Building *
*Handan Rd. 220, **Shanghai, China*
*Tel +86-021-65642863*
*Mobile +86-13916819745*
http://www.mendeley.com/profiles/yi-xin-liu/

Re: [flask]using matplotlib to render data to webpage directly

From:
Simon Sapin
Date:
2011-09-29 @ 09:28
 On Thu, 29 Sep 2011 17:23:30 +0800, 刘一新 wrote:
> Thanks, Simon
>
> Few minutes ago, I figured out the same method as your first one by
> reading a article on web2py and matplotlib (Di Pierro, M. "Web2Py for
> Scientific Applications" Comput. Sci. Eng. 2011, 64.)
>
> BTW: If the data I want to plot is changing, is there any way to
> refresh the image I just rendered? I think this is similar to play a
> streaming vedio.
>
> Eshin

 Hi,

 If you want to reload an image without reloading the whole page, try 
 something like this:

     my_image.src = my_image.src;

 in Javascript, with my_image being a DOM element.

 Regards,
-- 
 Simon Sapin