Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled

Introduction

The Python IDE Spyder has a plugin called Spyder notebook. Spyder notebook is a plugin that allows you to edit Jupyter Notebook within Spyder. Not limited to editing Notebooks, by connecting to the Spyder console, you can execute methods described in Jupyter Notebooks, check / rewrite variables, and check variables with the Variable Explorer. I would like to use Jupytext to save Jupyter Notebooks in py file format, but at the moment (2020/7 / In 19), when Jupytext is enabled on Spyder Notebook, an error occurs and saving / updating cannot be performed properly. [It is taken up in Issue] of Spyder notebook [https://github.com/spyder-ide/spyder-notebook/issues/272), and I think that it will be supported in the future.

In this article, I will post as a memorandum how to avoid the above error in the current environment. I think that it will be supported by the main body eventually, so I think that this article will be useless.

Purpose of this article

With Jupytext enabled, connect the Jupyter Notebook kernel to the Spyder console and enable Variable Explorer.

Confirmed environment

procedure

1. Open the Notebook you want to edit in Spyder Notebook

1) Open the Notebook pane.

image.png

2a) When opening an existing Notebook for editing

Follow the steps below to open the Notebook.

  1. Right-click on a blank area of Notebook → select Open ... Alternatively, select Open ... from the hamburger menu at the top right of the pane.
  2. Select and open the ipynb file in the file selection dialog image.png

2b) When editing with a new Notebook

Save ʻUntitled0.ipynb`, which is open in the initial state, to any location by following the procedure below.

  1. Right-click on the blank area of ʻUntitled0.ipynb` → select Save as ... Alternatively, select Save as ... from the hamburger menu at the top right of the pane.
  2. Specify the save destination of the file in the file selection dialog and save it.

image.png

2. Jupytext settings

(Settings when using Jupytext for the first time, unnecessary if already set)

1) Enable file saving in percent format

Check "File> Jupytext> Pair Notebook with percent Script" in Spyder Notebook to enable file saving in percentage format.

image.png

When you save, a .py file with the same file name as .ipynb is created in the same folder.

image.png

--Example - test_spyder_notebook.ipynb - test_spyder_notebook.py

3. Check the URL of Spyder Notebook

1) Check the running Jupyter Notebook server

ʻStart Anaconda prompt` and enter the following command to check the currently running Jupyter Notebook server.

Anaconda-prompt


jupyter notebook list

When the command is executed, the output will be as follows.

Output example


Currently running servers:
http://localhost:8888/?token=52a00d3ac13d903245ea6a85611667c45f19ea7d902d6e3d :: C:\Users\<user name>
http://localhost:8889/?token=d1e551f8c2627bccfd18f9e56a08035c800cd4ccb7f4097b :: C:\test

2) Open Notebook in your browser

The URL where the initial directory displayed at the end of each URL line and the directory where the Notebook is saved is the same (the second line in the example) is the URL of the Notebook server currently open in Spyder notebook. Copy all the URLs up to the token and open it in your browser.

image.png

3) Check Notebook

Check if the target Notebook (eg test_spyder_notebook.ipynb) exists in the list of Jupyter Notebooks on your browser. If the target Notebook cannot be confirmed, the URL may be incorrect. In that case, select another URL from the URL list confirmed on the console and check it again on the browser.

4) Close the Notebook on the Spyder Notebook

After confirming the target Notebook, ** close the Notebook on Spyder Notebook **. (The default screen of Spyder Notebook is just displayed.)

image.png

4. Open the percent format py file in Jupyter Notebook

1) Open the .py file in a Jupyter Notebook on your browser

--Supplement --When you open the .py file, the status will be Running and the file name will be green. --The .ipynb file should be grayed out because you closed it on spyder earlier.

image.png

image.png

5. Connect the Jupyter Notebook kernel to the Spyder console

1) Connect to an existing kernel

From the Spyder console hamburger menu, select Connect to Existing Kernel.

image.png

When the dialog opens, click the "View" button of the connection information file. image.png

A file selection dialog opens. Sort the files in order of modification date and select the JSON file with the latest update date.

image.png

--Supplement --This JSON file is the kernel connection information of the test_spyder_notebook.py file that you just opened in Jupyter Notebook. --The default connection information file (json file) is saved in C: \ Users \ <user name> \ AppData \ Roaming \ jupyter \ runtime.

Contents of json file (example)


{
  "shell_port": 49552,
  "iopub_port": 49553,
  "stdin_port": 49554,
  "control_port": 49555,
  "hb_port": 49556,
  "ip": "127.0.0.1",
  "key": "591c2e22-fc725f42989c69108b68f974",
  "transport": "tcp",
  "signature_scheme": "hmac-sha256",
  "kernel_name": ""
}

Do not change anything else and press OK to close the dialog. image.png

2) State after connection

You can see that the console has been added. (Here, the console name is "Console 2 / A") If you edited the Notebook before connecting to the console, the Variable Explorer will display the variables. image.png

6. Execution example

1) Input to Jupyter Notebook

You can use it as usual. image.png

2) Check the variable explorer

Because the contents of Jupyter Notebook are not reflected in real time In the initial state, no variables are displayed in the variable explorer. image.png

In this state, if you press the Enter key while leaving a blank line on the Spyder console, the changes will be reflected.

image.png

Once the changes are reflected, you will be able to see the variables in the Variable Explorer, just as you would normally use in Spyder.

Example: df_iris image.png

3) Editing py files on Spyder

Since Jupytext is used, the execution contents on Jupyter Notebook are saved in the py file at any time. Since the py file is a script file, it can be opened and edited with Spyder's editor.

image.png

important point

If there is a change on the Jupyter Notebook, the Spyder editor will automatically reload the py file, but if you change the py file on the Spyder editor, the Jupyter Notebook will not automatically reload it. If you want to reflect the changes made on the Spyder editor in the Jupyter Notebook, you need to reload the Jupyter Notebook page with the F5 key. (It seems that it can be handled by setting, but I do not know.)

Recommended Posts

Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
The usual way to add a Kernel with Jupyter Notebook
The kernel of jupyter notebook can no longer connect
I want to blog with Jupyter Notebook
In Jupyter, add IPerl to the kernel.
How to use jupyter notebook with ABCI
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)
How to debug with Jupyter or iPython Notebook
Fill the browser with the width of Jupyter Notebook
Add / remove kernel to use jupyter with venv
To output a value even in the middle of a cell with Jupyter Notebook
Monitor the training model with TensorBord on Jupyter Notebook
How to instantly launch Jupyter Notebook from the terminal
Next to Excel, for the time being, jupyter notebook
A solution to the problem that kernel restarting frequently occurs when running PyQt system with jupyter or Spyder IDE
How to deal with the phenomenon that Python (Jupyter notebook) executed on WSL becomes Aborted
Connect to BigQuery with Python
Introduced Jupyter Notebook to CentOS 7
Using Graphviz with Jupyter Notebook
Memo to get the value on the html-javascript side with jupyter
Use pip with Jupyter Notebook
Connect to Wikipedia with Python
Connect to Postgresql with GO
Reflect the virtual environment created with Miniconda in Jupyter notebook
Use Cython with Jupyter Notebook
I want to use a virtual environment with jupyter notebook!
How to use Jupyter Notebook
How to touch Jupyter Notebook without polluting the environment other than Pythonista, or how to touch Ruby with Jupyter Notebook without polluting the environment other than Rubyist
How to batch start a python program created with Jupyter notebook
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
Allow external connections with jupyter notebook
Formatting with autopep8 on Jupyter notebook
Connect to multiple databases with SQLAlchemy
Visualize decision trees with jupyter notebook
Make a sound with Jupyter notebook
[Day3] Preparing to connect to the database
Simply view the Jupyter notebook file
Connect to Bitcoin Testnet with Pycoin
Use markdown with jupyter notebook (with shortcut)
Add more kernels with Jupyter Notebook
Convenient analysis with Pandas + Jupyter notebook
Connect to Elastic MQ with boto
Easy to use Jupyter notebook (Python3.5)
I wanted to use jupyter notebook with docker in pip environment (opticspy)
Connect to VPN with your smartphone and turn off / on the server
I want to pin Spyder to the taskbar
Use nb extensions with Anaconda's Jupyter notebook
Markdown to get Jupyter notebook results to Qiita
Use apache Spark with jupyter notebook (IPython notebook)
Try to make a kernel of Jupyter
How to install DLIB with 2020 / CUDA enabled
Connect Sipeed Lichee Zero to the net
Use Jupyter Lab and Jupyter Notebook with EC2
Try SVM with scikit-learn on Jupyter Notebook
Connect to MySQL with Python within Docker
How to execute commands in jupyter notebook
Clone the github repository on jupyter notebook
Connect to GNU / Linux with Remote Desktop
To import your own module with jupyter
Linking python and JavaScript with jupyter notebook
Using Java's Jupyter Kernel with Google Colaboratory