Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed python tkinter window into a browser?

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!! :-)

like image 929
khan Avatar asked Jun 03 '12 23:06

khan


1 Answers

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.

like image 167
Arthur Endlein Avatar answered Sep 27 '22 19:09

Arthur Endlein