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.
With Jupytext enabled, connect the Jupyter Notebook kernel to the Spyder console and enable Variable Explorer.
Follow the steps below to open the Notebook.
Save ʻUntitled0.ipynb`, which is open in the initial state, to any location by following the procedure below.
(Settings when using Jupytext for the first time, unnecessary if already set)
Check "File> Jupytext> Pair Notebook with percent Script" in Spyder Notebook to enable file saving in percentage format.
When you save, a .py file with the same file name as .ipynb is created in the same folder.
--Example
- test_spyder_notebook.ipynb
- test_spyder_notebook.py
ʻ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
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.
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.
After confirming the target Notebook, ** close the Notebook on Spyder Notebook **. (The default screen of Spyder Notebook is just displayed.)
--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.
From the Spyder console hamburger menu, select Connect to Existing Kernel.
When the dialog opens, click the "View" button of the connection information file.
A file selection dialog opens. Sort the files in order of modification date and select the JSON file with the latest update date.
--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.
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.
You can use it as usual.
Because the contents of Jupyter Notebook are not reflected in real time In the initial state, no variables are displayed in the variable explorer.
In this state, if you press the Enter key while leaving a blank line on the Spyder console, the changes will be reflected.
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
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.
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