Search code examples
pythonsavejupyter

How to save python script as .py file on jupyter notebook


I wrote some functions on Jupyter notebook and want to save the script in form of .py, but anaconda jupyter notebook automatically saves the script as .ipynb, How can I change it to .py for this specific script? Is there any command for it? Thanks


Solution

  • There are built-in Magic commands in Jupyter:

    IPython has a set of predefined ‘magic functions’ that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.

    Magics are useful as convenient functions where Python syntax is not the most natural one, or when one want to embed invalid python syntax in their work flow.

    %%writefile (Write the contents of the cell to a file)

    %save (Save a set of lines or a macro to a given filename)