Skip to content
Snippets Groups Projects
Commit f739acd9 authored by HEMMERLE Arnaud's avatar HEMMERLE Arnaud
Browse files

Start migration

parent 81370f64
No related branches found
No related tags found
No related merge requests found
...@@ -3,28 +3,23 @@ Module dealing with actions on the notebook itself (create cell, save nb, ...). ...@@ -3,28 +3,23 @@ Module dealing with actions on the notebook itself (create cell, save nb, ...).
The code requires use of Javascript to modify the structure of the notebook The code requires use of Javascript to modify the structure of the notebook
and is therefore not usable in Jupyter Lab, which forbids Javascript. and is therefore not usable in Jupyter Lab, which forbids Javascript.
""" """
import time
import base64
from IPython.display import Javascript, display
from ipywidgets import Widget from ipywidgets import Widget
from ipylab import JupyterFrontEnd
app = JupyterFrontEnd()
def save_nb(): def save_nb():
""" """
Save the current state of the notebook (including the widgets). Save the current state of the notebook (including the widgets).
""" """
#display(HTML('<script>Jupyter.menubar.actions._actions["widgets:save-with-widgets"].handler()</script>') ) app.commands.execute('docmanager:save')
display(Javascript('IPython.notebook.save_checkpoint();'))
def delete_cell(): def delete_cell():
""" """
Delete the cell in which this function was called in the Jupyter Notebook. Delete the cell in which this function was called in the Jupyter Notebook.
""" """
display(Javascript( app.commands.execute('notebook:move-cursor-up')
""" app.commands.execute('notebook:delete-cell')
var index = IPython.notebook.get_selected_cells_indices();
IPython.notebook.delete_cell(index);
"""
))
def create_cell(code, position='below', celltype='markdown', def create_cell(code, position='below', celltype='markdown',
is_print=True, is_execute=True): is_print=True, is_execute=True):
...@@ -45,40 +40,28 @@ def create_cell(code, position='below', celltype='markdown', ...@@ -45,40 +40,28 @@ def create_cell(code, position='below', celltype='markdown',
Execute the cell after its creation Execute the cell after its creation
""" """
encoded_code = (base64.b64encode(code.encode('latin1'))).decode('utf8') if position == 'below':
app.commands.execute('notebook:insert-cell-below')
# Create a unique id for the cell based on epoch time if position == 'at_bottom':
time.sleep(0.1) # Delay to ensure unique id app.commands.execute('notebook:extend-marked-cells-bottom')
display_id = int(time.time()*1e9) app.commands.execute('notebook:insert-cell-below')
js_code = """var cell = IPython.notebook.insert_cell_{0}("{1}"); app.commands.execute('notebook:move-cursor-up')
cell.set_text(atob("{2}")); app.commands.execute('notebook:replace-selection', { 'text': code})
"""
if not is_print: if celltype == 'markdown':
js_code += """cell.metadata.tags = ['notPrint'] app.commands.execute('notebook:change-cell-to-markdown')
"""
if is_execute: if is_execute:
js_code += """cell.execute(); app.commands.execute('notebook:run-cell')
"""
display(Javascript(js_code.format(position, celltype, encoded_code)),display_id=display_id)
# Necessary hack to avoid self-execution of cells at notebook re-opening
display(Javascript(""" """), display_id=display_id, update=True)
def refresh_cell(): def refresh_cell():
""" """
Refresh the current cell. Refresh the current cell.
""" """
# Create a unique id for the cell based on epoch time app.commands.execute('notebook:run-cell')
display_id = int(time.time()*1e9)
display(Javascript("""IPython.notebook.execute_selected_cells();"""),display_id=display_id)
# Necessary hack to avoid self-execution of cells at notebook re-opening
display(Javascript(""" """), display_id=display_id, update=True)
def create_cell_action_widgets(is_delete_current_cell): def create_cell_action_widgets(is_delete_current_cell):
""" """
...@@ -95,8 +78,5 @@ def create_cell_action_widgets(is_delete_current_cell): ...@@ -95,8 +78,5 @@ def create_cell_action_widgets(is_delete_current_cell):
celltype='code', is_print=False, is_execute=True celltype='code', is_print=False, is_execute=True
) )
# Close all the active widgets to save memory
Widget.close_all()
if is_delete_current_cell: if is_delete_current_cell:
delete_cell() delete_cell()
...@@ -3,11 +3,15 @@ Custom module called by `JupyLabBook.ipynb`. ...@@ -3,11 +3,15 @@ Custom module called by `JupyLabBook.ipynb`.
Coordinates the different sub-modules. Coordinates the different sub-modules.
The object `experiment` containing all the attributes relevant to the current experiment is defined here. The object `experiment` containing all the attributes relevant to the current experiment is defined here.
""" """
from IPython.display import Javascript, display from IPython.display import display
from lib.frontend import action, process, experiment, notebook from lib.frontend import action, process, experiment, notebook
from ipylab import JupyterFrontEnd
app = JupyterFrontEnd()
# To have all the cells expanded (not collapsed) # To have all the cells expanded (not collapsed)
display(Javascript('IPython.OutputArea.prototype._should_scroll = function(lines) {return false;}')) app.commands.execute('notebook:disable-output-scrolling')
# Save pdf images for the final pdf report # Save pdf images for the final pdf report
try: try:
...@@ -22,7 +26,7 @@ except ModuleNotFoundError: ...@@ -22,7 +26,7 @@ except ModuleNotFoundError:
# Rules for numbering versions # Rules for numbering versions
# vX.Y.Z with X major, Y minor, Z patch. Each new version increments Z (not used if Z=0). # vX.Y.Z with X major, Y minor, Z patch. Each new version increments Z (not used if Z=0).
# If the file Example.ipynb cannot run with the new libraries, increment Y and restart at Z=0. # If the file Example.ipynb cannot run with the new libraries, increment Y and restart at Z=0.
__version__ = 'v3.2.3' __version__ = 'jupyterlab'
# Define the object experiment # Define the object experiment
# It contains all the attributes relevant to the current experiment # It contains all the attributes relevant to the current experiment
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment