In Jupyter Lab, execute editor code in Python console

喜夏-厌秋 提交于 2019-12-22 05:42:31

问题


In Jupyter Lab, I want to send code from the editor to the Python console for execution, preferably with a keyboard shortcut. The documentation doesn't seem to offer a way to do this, but it's such a fundamental aspect of an IDE that I imagine it's probably possible.


回答1:


This feature has been released now. All you need to do is

  1. Right click the script and create a console editor
  2. Copy the whole code and press shift + Enter



回答2:


This is planned as a feature for the 1.0 release. See https://github.com/jupyter/jupyterlab/issues/450




回答3:


This may be a workaround, but what you can do is open an interactive console session. This uses IPython, so then you can use the magic %run to execute a .py file. So do something like

%run ./yourFile.py



回答4:


The answer:

Select your desired line(s) and use Run > Run Selected Text or Current Line in Console, or define your own shortcut under Settings > Advanced Settings > Keyboard Shortcuts


The details:

1 - Send code from the editor to the Python console:

While the cell is active, click Run and select Run Selected Text or Current Line in Console.

Test run and output:

For those trying JupyterLab for the first time, this is opposed to the standard option of hitting ctrl+Enter and getting the output within JupyterLab itself:


2. How to do it using a keyboard shortcut

There's no standard shortcut for this, but you can quite easily set it up yourself if you follow these few easy steps:

1 - Go to Settings and select Advanced Settings editor:

2 - Under the appearing Settings Tab, run a ctrl+F search for run-in-console to locate the following section:

// [missing schema title]
    // [missing schema description]
    "notebook:run-in-console": {
      "command": "notebook:run-in-console",
      "keys": [
        ""
      ],
      "selector": ".jp-Notebook.jp-mod-editMode",
      "title": "Run In Console",
      "category": "Notebook Cell Operations"
    }

3 - Copy that part and paste it under User Overrides and type in your desired shortcut below keys:

As you can see, my preferred shortcut is F9.

4 - Click Save All under File.

And if you close and reopen your notebook, you'll see that you've assigned F9 as a shortcut in the menu itself:

5.1 - Run single line / Send single line to IPython console.

Just put your marker on the desired line and click F9:

5.2 - Run selected code / send selected text to IPython console:

Just select your desired code and click F9




回答5:


This can also be done for a single line by hitting shift + enter with the cursor on that line (as opposed to selecting the entire line, then hitting shift + enter).



来源:https://stackoverflow.com/questions/38648286/in-jupyter-lab-execute-editor-code-in-python-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!