0

How to convert restructured text (.rst file) to jupyter notebook (.ipynb file)?

I have searched and tried the package rst2ipynb, but running the command rst2ipynb causes Windows to ask me "open with ...". I don't know other options. I prefer to use Jupyter's official solutions, because I know rst file is one of the natively supported formats that Jupyter supports.

Neo
  • 1,031
  • 2
  • 11
  • 27
  • 1
    That 'Open with...' question doesn't necessarily mean the conversion didn't work. You'd have to share an actual result here or more forward thinking, try the notebook in Jupyter on systems other than yours. There's several around that I can point you at later. You should be able to actually open the file as text in a text editor and see if it worked if you look at some 'good' notebooks and compare. Besides the tool you use, you can convert from rst to markdown with pandoc. Then easily to an ipynb. (nbformat can be used in that path, too.) I think Quarto may offer a path too but I'm not sure. – Wayne May 02 '23 at 12:37
  • I really appreciate this thoughtful answer but I am sorry that I still don't get it. Can you tell me what is a suggested way to convert rst to ipynb? – Neo May 02 '23 at 13:54
  • 1
    I wanted to point you at some directions in case you knew some of the tools I mentioned. I didn't have time to do a step-by-step. I'll work on an example. Separate from that, I'd like to address your statement, " because I know rst file is one of the natively supported formats that Jupyter supports." What is this statement based on? Based on [this post](https://github.com/jupyter/help/issues/241#issuecomment-342846561), which I found linked to from [here](https://stackoverflow.com/q/59726350/8508004) that may be useful to you, that isn't the case. There's definitely solutions but not .... – Wayne May 02 '23 at 14:44
  • official support as far as I know. – Wayne May 02 '23 at 14:44
  • I make the statement because I see myself that jupyter lab supports `export to rst` option. I think this suggests there is some strong link between ipynb and rst. – Neo May 02 '23 at 14:55

1 Answers1

1

Using sphinxcontrib.jupyter

As suggested by the rst2ipynb developer here.

"A better approach would be to implement a proper Jupyter-export extension for Sphinx.
NEWS (2017-10): you may want to explore the recent https://github.com/QuantEcon/sphinxcontrib-jupyter/ that does just this."

That tool has a section covering 'Usage in RST Files' that says "minimum configured sphinx repo is available here which generates a sample notebook".

That repository is sphinxcontrib-jupyter.minimal. And because it is all set up, you can easily plug into that for processing the conversions of your own .rst files.

This makes it easier because the configuration conf.py is already set up to convert any .rst file in the same directory to an .ipyn file when make jupyter is run.

Step-by-step trying sphinxcontrib-jupyter.minimal

Start up a remote, temporary Jupyter session with the content of 'sphinxcontrib-jupyter.minimal'.

Go here and click 'Launch binder'.

Use the Launcher pane to open a terminal in the Jupyter session.

In the terminal, run:

git clone https://github.com/QuantEcon/sphinxcontrib-jupyter.minimal.git
cd sphinxcontrib-jupyter.minimal/

(The cd line above will change the terminal working directory to be in the sphinxcontrib-jupyter.minimal directory. This only affects the terminal and not the rest of Jupyter, for now.)

Install Sphinx and the extension.

Run in the terminal

pip install sphinxcontrib-jupyter

Navigate into the sphinxcontrib-jupyter.minimal directory in Jupyter's file navigation panel to prepare to add your own .rst file that will be converted.

To go into the sphinxcontrib-jupyter.minimal double-click on it in the file navigation pane, and you notice the indicator of the level in the hierarchy just above where you clicked will change to indicate where you are now.

In the sphinxcontrib-jupyter.minimal directory there is already two .rst files that these steps will convert. You should add your own to convince yourself it works with any .rst file.

Drag-and-drop from your local computer your .rst file to convert into file navigation pane on the left side of the Jupter session window. It will upload the file to the remote session.

My test file was this README.rst:

https://github.com/RunestoneInteractive/RunestoneComponents/blob/master/README.rst

I downloaded the raw code of that README file and renamed it RunestoneComponents.rst.

You should be ready to run the conversion process now. Any .rst file in that sphinxcontrib-jupyter.minimal directory will be converted in the next step.

Run the conversion of the .rst files

Run in the terminal

make jupyter

Importantly, as noted at the bottom of the text here:

"Generated notebooks can be found in the _build/jupyter/ folder."

Use the file navigation pane on the left and go into that _builddirectory and then the jupyter subdirectory and then you can click to open the converted notebooks.
Or you can right-click and use Open with... to handle as you see fit.

The session is temporary so in the file navigation pane, right-click and choose to download back to your local computer any useful notebook file you make on the remote session.

You'll see the result of the simple_notebook.rst conversion isa much more full-featured example converted notebook file. and you can use that to guide you in how to adjust your .rst input if necessary.



Alternatively, there is rst2ipynb...

Using rst2ipynb to convert step-by-step

Start up a remote, temporary Jupyter session by going here and clicking 'Launch binder'.

Use the Launcher pane to open a terminal in the Jupyter session.

Install pandoc as suggested here

In the terminal, type:

conda install -c conda-forge pandoc -y

Next, install rst2ipynb.

In the same terminal, run:

git clone https://github.com/nthiery/rst-to-ipynb.git
cd rst-to-ipynb
pip install .
cd ..

The last command just goes back up a level in the directory hierarchy.

Drag-and-drop from your local computer your .rst file to convert into file navigation pane on the left side of the Jupter session window. It will upload the file to the remote session.

My test file was this README.rst:

https://github.com/RunestoneInteractive/RunestoneComponents/blob/master/README.rst

I downloaded the raw code of that README file and renamed it RunestoneComponents.rst.

Run the conversion

In terminal, I ran:

rst2ipynb RunestoneComponents.rst -o test.ipynb

Substitute the file name of your .rst file for RunestoneComponents.rst. Make the test.ipynb name whatever you want.

Double click on the created .ipynb file in the file navigation pane and it will open as a notebook in the session. You can right-click to choose Open With... options if you prefer.
The session is temporary so in the file navigation pane, right-click and choose to download back to your local computer any useful notebook file you make on the remote session.

rst2ipynb comes with its own test content you can convert, too.

rst2ipynb rst-to-ipynb/tests/all.rst -o all_test.ipynb

You'll see this results in a much more full-featured example converted notebook file. and you can use that to guide you in how to adjust your .rst input if necessary.

How rst2ipynb conversion worked:

You'll note the text there after, "This is currently achieved by", nicely outlines the process I suggested in my comments:

"This is currently achieved by converting to markdown using pandoc and then to a Jupyter notebook ..."



Other alternatives probably exist these days...

These days for working with reStructuredText there is a way to migrate from reStructuredText to MyST Markdown that works with Jupyter Books. And Jupyter Books do allow using because they Sphinx under the hood, see here. And so there may well be a way using all that but I didn't see it written out concisely like the other two I highlight above.
Similar for Quarto.
If I come across something, I'll try to update this section

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • Strange. I run `rts2ipynb` with my rst file or the provided tests, all I get is just "Open with..." immediately and if I choose vscode, it just shows the rst2ipynb file itself... – Neo May 03 '23 at 00:11
  • That is odd. It would seem not to be in your path correctly? Can you open a Jupyter notebook and try the command in there with an exclamation point in front of the full command? MAke sure you repeat any `pip install` steps in the notebook with the magic `%pip install` variant. Restart the kernel and then try. I don't know enough about either one to know if they allow you to run them via a Python bases API either. If they did, you can run the Python inside a notebook instead of the command line. – Wayne May 03 '23 at 02:25