Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript Error: IPython is not defined #8415

Closed
giswqs opened this issue May 12, 2020 · 5 comments
Closed

Javascript Error: IPython is not defined #8415

giswqs opened this issue May 12, 2020 · 5 comments
Labels
status:resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.
Milestone

Comments

@giswqs
Copy link

giswqs commented May 12, 2020

I am using the following code to create a new cell programmatically. It works in Jupyter notebook, but I am getting an error Javascript Error: IPython is not defined in JupyteLab. Any advice? Thanks.

def create_code_cell(code='', where='below'):
    """Creates a code cell in the IPython Notebook.

    Args:
        code (str, optional): Code to fill the new code cell with. Defaults to ''.
        where (str, optional): Where to add the new code cell. It can be one of the following: above, below, at_bottom. Defaults to 'below'.
    """

    import base64
    from IPython.display import Javascript, display
    encoded_code = (base64.b64encode(str.encode(code))).decode()
    display(Javascript("""
        var code = IPython.notebook.insert_cell_{0}('code');
        code.set_text(atob("{1}"));
    """.format(where, encoded_code)))

content = 'print("Hello World")'
create_code_cell(code=content)
@jasongrout
Copy link
Contributor

The Javascript interface is completely different in JupyterLab: the IPython global javascript object does not exist.

We do support the payload way of adding a new code cell immediately after the current cell. See https://discourse.jupyter.org/t/how-to-programmatically-add-serveral-new-cells-in-a-notebook-in-jupyterlab/4323

@jasongrout jasongrout added this to the Reference milestone May 12, 2020
@giswqs
Copy link
Author

giswqs commented May 12, 2020

@jasongrout Thanks for the info. I need to create a new cell programmatically in combination with ipywidgets. The first code block below works without ipywidgets. However, when the function is being called within a ipywidget event, it won't create a new cell. @SylvainCorlay

That JavaScript function I posted earlier works with ipywidgets in Jupyter notebook. I am looking for a solution in JupyterLab.

def create_new_cell(contents, replace=False):

    from IPython.core.getipython import get_ipython
    shell = get_ipython()
    shell.set_next_input(contents, replace=replace)
create_new_cell(contents='print("Hello World!")')
from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)

def on_button_clicked(b):
    create_new_cell(contents='print("Hello World!")')

button.on_click(on_button_clicked)

@jasongrout
Copy link
Contributor

I think payloads only work with normal cell execute requests, not with the comm messages that ipywidgets uses.

@jasongrout
Copy link
Contributor

I think there is no supported way in JupyterLab to change the notebook cells from python code (e.g., add a cell) other than the deprecated payload option in normal cell execution.

@giswqs
Copy link
Author

giswqs commented May 12, 2020

@jasongrout Thanks for the info. In this case, I will have to use Jupyter notebook.

@giswqs giswqs closed this as completed May 12, 2020
@lock lock bot added the status:resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion. label Jun 24, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status:resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.
Projects
None yet
Development

No branches or pull requests

2 participants