Created
September 10, 2014 09:37
-
-
Save jvkersch/e874a1475257b203472f to your computer and use it in GitHub Desktop.
Quickly open a web page to display a variable in IPython
This file contains 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
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