Linking to multiple dynamic images from same web page

219 views
Skip to first unread message

Derek

unread,
Jan 18, 2015, 8:54:50 AM1/18/15
to django-users
I have a situation which is puzzling.

I have a web page that, when generated embeds links that look like:

</div><img src="/analysis/design/chart?field=A></div>
</div><img src="/analysis/design/chart?field=B></div>
</div><img src="/analysis/design/chart?field=C></div>

Each of these links points to a view that, when activated, creates a small PNG image (matplotlib is used for this) which is returned via an InMemoryUploadedFile object. 

The basic logic and image construction works - if each of these links is called directly from the browser, then each is created perfectly.

However, when embedded in a single page (as above) then only one single image is returned and the rest generate a similar error:

  File "/home/gamesbook/.virtualenvs/dev/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1401, in xlabel
    l =  gca().set_xlabel(s, *args, **kwargs)
  File "/home/gamesbook/.virtualenvs/dev/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 803, in gca
    ax =  gcf().gca(**kwargs)
  File "/home/gamesbook/.virtualenvs/dev/local/lib/python2.7/site-packages/matplotlib/figure.py", line 1221, in gca
    return self.add_subplot(1, 1, 1, **kwargs)
  File "/home/gamesbook/.virtualenvs/dev/local/lib/python2.7/site-packages/matplotlib/figure.py", line 916, in add_subplot
    self._axstack.add(key, a)
  File "/home/gamesbook/.virtualenvs/dev/local/lib/python2.7/site-packages/matplotlib/figure.py", line 120, in add
    Stack.remove(self, (key, a_existing))
  File "/home/gamesbook/.virtualenvs/dev/local/lib/python2.7/site-packages/matplotlib/cbook.py", line 1343, in remove
    raise ValueError('Unknown element o')
ValueError: Unknown element o

I have googled for this error, but it seems fairly obscure and I am not sure how it relates to my situation.

Any insights welcome!

Thanks
Derek

Mitesh Patel

unread,
Jan 18, 2015, 9:05:45 AM1/18/15
to django...@googlegroups.com
Hi Derek,

I face same issue few months ago, what I did is I destroyed plot object completely everytime the view is called and regenerated new plot everytime. You can try that. There is an issue with matplotlib memory, which basically usage same plot.

Another solution could be try different cStringIO to store base64 encoded image data, send that as response and at browser using javascript (data:image/png;base64).



Thanks,
With Best Regards,

Mitesh Patel

Senior Product Engineer

Mobile: +91-9970 8575 39
Email: mitesh.p...@gmail.com
LinkedIn : http://in.linkedin.com/in/miteshpatel11


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF1Wu3OfN9rmUKTDO%2BT%3DJan%3DF%3DLKODqQObds-tvME%3DUhL18fMw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Derek

unread,
Jan 19, 2015, 9:08:50 AM1/19/15
to django...@googlegroups.com
Thanks Mitesh

I thought it might be along those lines.  I have tried calling plt.close() just before I return from the plot creation function, but still get the same error.

Do you perhaps have a simple code example I can try and follow?  (I am already using the io module for the image storage.)

Derek
Reply all
Reply to author
Forward
0 new messages