##############################################################################
# Institute for the Design of Advanced Energy Systems Process Systems
# Engineering Framework (IDAES PSE Framework) Copyright (c) 2018, by the
# software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory, National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia
# University Research Corporation, et al. All rights reserved.
#
# Please see the files COPYRIGHT.txt and LICENSE.txt for full copyright and
# license information, respectively. Both files are also available online
# at the URL "https://github.com/IDAES/idaes".
##############################################################################
"""
Simple GUI utility functions
"""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import #disable implicit relative imports
__author__ = "John Eslick"
__version__ = "1.0.0"
from IPython.display import Javascript, display
from IPython.utils.py3compat import str_to_bytes, bytes_to_str
import base64
[docs]def insert_jupyter_code(code=''):
encoded_code = bytes_to_str(base64.b64encode(str_to_bytes(code)))
display(Javascript("""
var code = IPython.notebook.get_selected_cell()
if(!code.get_text() == ""){{
var code = IPython.notebook.insert_cell_at_bottom('code');
}}
code.set_text(atob("{}"));
var index = IPython.notebook.get_selected_index();
while (index !== (IPython.notebook.ncells()-1) && index !== null) {{
IPython.notebook.select_next(true);
index = IPython.notebook.get_selected_index();
}}
IPython.notebook.focus_cell();
IPython.notebook.insert_cell_at_bottom('code');
IPython.notebook.execute_cell_and_select_below();
""".format(encoded_code)))
[docs]def kernel_interupt():
display(Javascript("IPython.notebook.kernel.interrupt();"))
[docs]def kernel_execute(code):
display(Javascript("IPython.notebook.kernel.execute('{}');".format(code)))
[docs]def run_cells_below():
display(Javascript("IPython.notebook.execute_cells_below();"))
[docs]def cut_cell():
display(Javascript("""
var index = IPython.notebook.get_selected_index();
IPython.notebook.cut_cell();
IPython.notebook.select(index);
"""))
[docs]def move_cell_up():
display(Javascript("IPython..notebook.move_cell_up();"))
[docs]def move_cell_down():
display(Javascript("IPython.notebook.move_cell_down();"))
[docs]def add_cell():
display(Javascript("""
IPython.notebook.insert_cell_below();
IPython.notebook.select_next(true);
IPython.notebook.focus_cell();
"""))
[docs]def save_notebook():
display(Javascript("IPython.notebook.save_notebook();"))
[docs]def close_page():
display(Javascript("window.close();"))