7

Okay..the output of a python program is shown into a Tkinter window..which opens separately. What I want to do is to embed this window within the browser. Here is the code:

import numpy as np
import matplotlib
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt

datafile = cbook.get_sample_data('logo2.png', asfileobj=False)

print 'loading', datafile

im = image.imread(datafile)

im[:,:,-1] = 0.5  # set the alpha channel

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange')

ax.grid()

fig.figimage(im, 10, 10)

plt.show()

Consider that all the variables are input parameters that are given from a browser form field. Please help!! :-)

user545424
  • 15,713
  • 11
  • 56
  • 70
khan
  • 7,005
  • 15
  • 48
  • 70
  • http://matplotlib.sourceforge.net/faq/howto_faq.html#matplotlib-in-a-web-application-server and http://stackoverflow.com/questions/5515278/plot-matplotlib-on-the-web may be helpful. – Ken Jun 04 '12 at 00:46
  • that is a bit helpful, thanks ken. however, what i exactly need is the interactive functions of tkinter (which come by default) to be available on the web with the resulting pictures. How can I do that? :-) – khan Jun 04 '12 at 03:48
  • 1
    What you need is a (standard) backend that work in a browser. I don't know of any such backend, but I would not expect tKinter to be one of them... – Juh_ Jun 04 '12 at 09:47

1 Answers1

7

Based on Joe Kington's answer to a similar question, the backend mplh5canvas is possibly what you are looking for. Adapting your example code to work with it,

import numpy as np
import matplotlib
import mplh5canvas

matplotlib.use('module://mplh5canvas.backend_h5canvas')

import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt


fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange')

ax.grid()

plt.show(open_plot=True)

It seems to work well with the kind of interactivity you are looking for, even allowing animation. Having said that, it doesn't support every browser, as described in it's installation wiki page. If it is acceptable to restrict use to Chrome, Safari or Opera (in this case with some configuration, check that page) then it should suit you well, though might need some experimentation as well.

Community
  • 1
  • 1
Arthur Endlein
  • 126
  • 2
  • 5
  • Arthur, Thanks a million bro. I implemented this locally and it is working fine (although taking few seconds before opening the chrome tab). However, when uploaded on a server and tried running it through a web page (which executes the python file using php exec() function) it is not working i.e. not opening the result tab in my chrome. Any suggestions? – khan Jun 16 '12 at 05:56