Search code examples
pythonjupyter-notebookjupytercopy-paste

Is there any straightforward way to change part of copied text (with python code) into Markdown and Code cell in Jupyter notebook


I copied a long text from a webpage including several Python code sections and text sections into my Jypter notebook. Below what I copied and pasted. enter image description here

Is there any straightforward way to convert the code sections to executable Code cell and leave the rest text as Markdown instead of inserting new cell one by one for the code sections? (codes and texts are in separated paragraphs). Below what I want:

enter image description here

Thank you for your time in advance.


Solution

  • If it is just a few lines as in your example, the manual way is most convenient. Paste everything in one cell, and then split the cell by using a keyboard shortcut for that (Ctr-Shift-Minus) and use another keyboard shortcut (M) to change the types of cells with comments in it to Markdown.

    If you have lots of material to convert, Jupytext might help. https://github.com/mwouts/jupytext .