Skip to content

Instantly share code, notes, and snippets.

@jvkersch
Created September 10, 2014 09:37
Show Gist options
  • Save jvkersch/e874a1475257b203472f to your computer and use it in GitHub Desktop.
Save jvkersch/e874a1475257b203472f to your computer and use it in GitHub Desktop.
Quickly open a web page to display a variable in IPython
import webbrowser
from tempfile import NamedTemporaryFile
from IPython.core.magic import Magics, magics_class, line_magic
@magics_class
class BrowserMagics(Magics):
@line_magic
def browse(self, content):
"""Write content to a temporary file and open in browser.
Parameters
----------
content : str
A variable name or string literal to display.
"""
try:
data = self.shell.user_ns[content]
except KeyError:
data = content
tmp = NamedTemporaryFile(dir='.', suffix='.html', delete=False)
tmp.write(data)
webbrowser.open_new('file://' + tmp.name)
ip = get_ipython() # noqa
ip.register_magics(BrowserMagics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment