Source code for jyquickhelper.helper_in_notebook
# -*- coding: utf-8 -*-
"""
Functions to call from the notebook
:githublink:`%|py|6`
"""
from IPython.display import Javascript, HTML
from IPython.core.display import display as jdisp
[docs]def set_notebook_name_theNotebook(name="theNotebook", display=True):
"""
This function must be called from the notebook
you want to know the name. It relies on
a javascript piece of code. It populates
the variable ``theNotebook`` with the notebook name.
:param name: name of the variable to create
:param display: calls `display <http://ipython.readthedocs.io/en/stable/api/generated/
IPython.display.html#IPython.display.display>`_
or returns a javascript object
:return: None or `Javascript <http://ipython.readthedocs.io/en/stable/api/generated/
IPython.display.html#IPython.display.Javascript>`_
This solution was found at
`How to I get the current IPython Notebook name
<http://stackoverflow.com/questions/12544056/how-to-i-get-the-current-ipython-notebook-name>`_.
The function can be called in a cell.
The variable ``theNotebook`` will be available in the next cells.
Try function :func:`store_notebook_path` if this one does not work.
:githublink:`%|py|32`
"""
code = """var kernel = IPython.notebook.kernel;
var body = document.body, attribs = body.attributes;
var command = "__NAME__ = " + "'"+attribs['data-notebook-name'].value+"'";
kernel.execute(command);""".replace(" ", "").replace("__NAME__", name)
def get_name():
"return name"
j = Javascript(code)
if display:
jdisp(j)
return j
return get_name()
[docs]def load_extension(name):
"""
install an extension, checks first it exists,
if not displays an exception with the list of them
:param name: extension name
:githublink:`%|py|255`
"""
return Javascript("IPython.utils.load_extensions('%s')" % name)