Last active
May 7, 2016 08:04
-
-
Save miyakogi/aaf9107ae3e36c26f2ab903a8bc552f4 to your computer and use it in GitHub Desktop.
wdomでグラフをプロットするサンプル(とりあえず)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import re | |
import asyncio | |
from functools import partial | |
from wdom.document import get_document | |
from wdom.server import get_app, start_server, stop_server | |
from wdom.tag import H1, Div, Button | |
from bokeh.plotting import figure | |
from bokeh.embed import components | |
class Plot(Div): | |
def plot(self, figure): | |
script_tag, div = components(figure) | |
self.innerHTML = div | |
m = re.search(r'<script .*?>(.*)</script>', script_tag, re.S) | |
if m: | |
script = m.group(1) | |
self.exec(script) | |
def show_plot(plot_area, event): | |
plot = Div(parent=event.target.ownerDocument.body) | |
x = [1, 2, 3, 4, 5] | |
y = [4, 3, 6, 3, 4] | |
p = figure(title='Bokeh test', x_axis_label='x', y_axis_label='y') | |
p.line(x, y, legend='Temp.', line_width=2) | |
plot_area.plot(p) | |
def main(): | |
doc = get_document(reload_wait=2000) | |
doc.add_cssfile('http://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.css') | |
doc.add_jsfile('http://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.js') | |
doc.body.append(H1('Bokeh Sample')) | |
plot_area = Plot(parent=doc.body) | |
btn = Button('show', parent=doc.body) | |
btn.addEventListener('click', partial(show_plot, plot_area)) | |
app = get_app(doc) | |
server = start_server(app) | |
loop = asyncio.get_event_loop() | |
try: | |
loop.run_forever() | |
except KeyboardInterrupt: | |
pass | |
finally: | |
stop_server(server) | |
loop.close() | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import os | |
import shutil | |
import tempfile | |
import atexit | |
import asyncio | |
from wdom.document import get_document | |
from wdom.server import get_app, start_server, stop_server | |
from wdom.tag import H1, Div, Button, Img | |
import numpy as np | |
import matplotlib as mpl | |
mpl.use('Agg') # must set before importing pyplot | |
import matplotlib.pyplot as plt | |
import seaborn as sns # enable seaborn | |
def show(event): | |
doc = event.target.ownerDocument | |
plot = Div(parent=doc.body) | |
x = [1, 2, 3, 4, 5] | |
y = [4, 3, 6, 3, 4] | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
x = np.linspace(0, 14, 100) | |
for i in range(1, 7): | |
ax.plot(x, np.sin(x + i * 0.5) * (7 - 1)) | |
tmpfile = os.path.join(doc.tempdir, tempfile.gettempprefix() + '.png') | |
fig.savefig(tmpfile) | |
plot.append(Img(src='tmp/' + tmpfile)) | |
def cleanup(doc): | |
if os.path.exists(doc.tempdir): | |
shutil.rmtree(doc.tempdir) | |
def main(): | |
doc = get_document(reload_wait=2000) | |
doc.tempdir = tempfile.mkdtemp() | |
atexit.register(cleanup, doc) | |
doc.body.append(H1('Seaborn Sample')) | |
btn = Button('show', parent=doc.body) | |
btn.addEventListener('click', show) | |
app = get_app(doc) | |
if os.path.exists(doc.tempdir): | |
app.add_static_path('tmp', doc.tempdir, no_watch=True) | |
server = start_server(app) | |
loop = asyncio.get_event_loop() | |
try: | |
loop.run_forever() | |
except KeyboardInterrupt: | |
pass | |
finally: | |
stop_server(server) | |
loop.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment