Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pym embedding #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lightning/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ def enable_ipython(self, **kwargs):
self.ipython_enabled = True
ip = get_ipython()
formatter = ip.display_formatter.formatters['text/html']
formatter.for_type(Visualization, lambda viz, kwds=kwargs: viz.get_html())
formatter.for_type(Visualization, lambda viz, kwds=kwargs: viz.get_pym_html())

r = requests.get(self.get_ipython_markup_link(), auth=self.auth)
display(Javascript(r.text))

r = requests.get("https://cdnjs.cloudflare.com/ajax/libs/pym/0.4.5/pym.js")
display(Javascript(r.text))

def disable_ipython(self):
Expand Down
15 changes: 11 additions & 4 deletions lightning/visualization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import json
import uuid
import webbrowser


Expand All @@ -16,7 +17,6 @@ def __init__(self, session=None, json=None, auth=None):
self.comm_handlers = {}
self.comm.on_msg(self._handle_comm_message)


def _format_url(self, url):
if not url.endswith('/'):
url += '/'
Expand Down Expand Up @@ -68,14 +68,23 @@ def get_html(self):
r = requests.get(self.get_embed_link(), auth=self.auth)
return r.text

def get_pym_link(self):
return self._format_url(self.get_permalink() + '/pym')

def get_pym_html(self):
r = self.get_pym_link()
uid = uuid.uuid4().hex
html = '<div id="%s" style="width: 500px"></div>' % uid
js = '<script> var pymParent = new pym.Parent("%s", "%s", {}); </script>' % (uid, r)
return html + js

def open(self):
webbrowser.open(self.session.host + '/visualizations/' + str(self.id) + '/')

def delete(self):
url = self.get_permalink()
return requests.delete(url)


def on(self, event_name, handler):

if self.session.lgn.ipython_enabled:
Expand All @@ -84,15 +93,13 @@ def on(self, event_name, handler):
else:
raise Exception('The current implementation of this method is only compatible with IPython.')


def _handle_comm_message(self, message):
# Parsing logic taken from similar code in matplotlib
message = json.loads(message['content']['data'])

if message['type'] in self.comm_handlers:
self.comm_handlers[message['type']](message['data'])


@classmethod
def create(cls, session=None, data=None, images=None, type=None, options=None):

Expand Down