Ejemplo n.º 1
0
def example_notebook(extra_flags_as_one_string=None):
    r"""
    Open the example notebook in the default browser.
    The ipython terminal stays active while the notebook is still active. Ctr+C in the ipython terminal will
    close the notebook.

    Note: This notebook is a working copy of the original notebook. Feel free to mess around with it
    
    """
    from IPython.terminal.interactiveshell import TerminalInteractiveShell
    import shutil, os
    origfile = _molpxdir('notebooks/Projection_Explorer.ipynb')

    with TemporaryDirectory(suffix='_test_molpx_notebook') as tmpdir:
        tmpfile = os.path.abspath(
            os.path.join(tmpdir, 'Projection_Explorer.ipynb'))
        shutil.copy(origfile, tmpfile)

        nbstring = open(tmpfile).read()
        f = open(tmpfile, 'w')
        f.write(
            nbstring.replace(
                "# molPX intro", "<font color='red', size=1>"
                "This is a temporary copy of the original notebook found in `%s`. "
                "This temporary copy is located in `%s`. "
                "Feel free to play around, modify or even break this notebook. "
                "It wil be deleted on exit it and a new one created next time you issue "
                "`molpx.example_notebook()`</font>\\n\\n"
                "# molPX intro" % (origfile, tmpfile)))
        f.close()
        cmd = 'jupyter notebook %s' % tmpfile
        if isinstance(extra_flags_as_one_string, str):
            cmd = '%s %s' % (cmd, extra_flags_as_one_string)

        eshell = TerminalInteractiveShell()
        eshell.system_raw(cmd)