Package: p2j
p2j - Python to Jupyter Notebook ParserConvert your Python source code to Jupyter notebook withzero intervention.Here is an example.# Evaluate the modelmodel.evaluate()# Run the model for a while.# Then we hide the model.run()hide()print(type(data))# This is considered as a paragraph too# It has 2 lines of comments# The data that we are interested in is made of 8x8 images of digits.# Let's have a look at the first 4 images, which is of course# stored in the `images` attribute of the dataset.images=list(zip(mnist.images))which translates to the following:Here's another example of aPython source codeand itsJupyter notebookafter converting.The purpose of this package is to be able to run a code on Jupyter notebook without having to copy each paragraph of the code into every cell. It's also useful if we want to run our code in Google Colab. This parser isn't perfect, but you would be satisfactorily pleased with what you get.Contents of this README:InstallingConvertingTestsRequirementsCode formatHow it worksFeedback and pull requestsInstallationPyPIpipinstallp2jPython's setup.pypythonsetup.pyinstallorpipinstallgit+https://github.com/remykarem/python2jupyter#egg=p2jConvertingThere are 3 main ways you can get your Jupyter notebook:Converting a Python scriptp2jtrain.pyand you will get atrain.ipynbJupyter notebook.Converting a script from the Internet (you need to have curl)Specify the target filename with a-t.p2j<(curlhttps://raw.githubusercontent.com/keras-team/keras/master/examples/mnist_cnn.py)-tmyfile.ipynbConverting an in-line Python scriptp2j<(echo"# boilerplate code \n import os")-tmyfile2.ipynbNote:To run examples from this repository, first clone this repogitclonehttps://github.com/raibosome/python2jupyter.gitand after youcdinto the project, runp2jexamples/example.pyThep2j/examples/example.pyis a Keras tutorial on building an autoencoder for the MNIST dataset, foundhere.Command line usageTo see the command line usage, runp2j -hand you will get something like this:usage: p2j [-h] [-r] [-t TARGET_FILENAME] [-o] source_filename

Convert a Python script to Jupyter notebook

positional arguments:
  source_filename       Python script to parse

optional arguments:
  -h, --help            show this help message and exit
  -r, --reverse         To convert Jupyter to Python script
  -t TARGET_FILENAME, --target_filename TARGET_FILENAME
                        Target filename of Jupyter notebook. If not specified,
                        it will use the filename of the Python script and
                        append .ipynb
  -o, --overwrite       Flag whether to overwrite existing target file.
                        Defaults to falseRequirementsPython >= 3.6No third party libraries are used.TestsTested on macOS 10.14.3 with Python 3.6.Code formatThere is no specific format that you should follow, but generally the parser assumes a format where your code is paragraphed. Check out some examples of well-documented code (and from which you can test!):PyTorch TutorialsKeras ExamplesScikit Learn ExampleHow it worksJupyter notebooks are just JSON files, like below. A Python script is read line by line and a dictionary of key-value pairs are generated along the way, using a set of rules. Finally, this dictionary is dumped as a JSON file whose file extension is.ipynb.{"cells":[{"cell_type":"markdown","execution_count":null,"metadata":{},"outputs":[],"source":["# Import standard functions"]},{"cell_type":"code","metadata":{},"source":["import os"]},],"metadata":{},"nbformat":4,"nbformat_minor":2}There are 4 basic rules (and exceptions) that I follow to parse the Python script.1. Code or commentFirstly, any line that starts with a#is marked as a comment. So this will be amarkdown cellin the Jupyter notebook. Everything else that does not start with this character is considered code, so this goes to thecode cell. There are of course exceptions.This is a comment# Train for 4 epochsand this is codemodel.train(4)2. Blocks of code and commentSecondly, code or comment can occur in blocks. A block of comment is severalconsecutivelines of comments that start with#. Similarly, severalconsecutivelines of codes that do not start with#will be considered as 'a block of code'. This rule is important because we want to ensure that a block of code or comment stays in one cell.This is a block of comment# Load the model and# train for 4 epochs and# lastly we save the modeland this is a block of codemodel.load()model.train(4)model.save()3. ParagraphThirdly, I assume that everyone writes his/her script in paragraphs, where each paragraph represents an idea. In a paragraph, there can be code or comments or both.The following are 5 examples of paragraphs.# Evaluate the modelmodel.evaluate()# Run the model for a while.# Then we hide the model.run()hide()print(type(data))# This is considered as a paragraph too# It has 2 lines of comments# The data that we are interested in is made of 8x8 images of digits.# Let's have a look at the first 4 images, which is of course# stored in the `images` attribute of the dataset.images=list(zip(mnist.images))which translates to the following:4. IndentationAny line of code or comment that is indented by a multiple of 4 spaces is considered code, and will stay in the same code cell as the previous non-empty line. This ensures that function and class definitions, loops and multi-line code stay in one cell.5. ExceptionsNow we handle the exceptions to the above-mentioned rules.Docstrings are considered asmarkdown cells, only if they are not indented.Lines that begin with#pylintor# pylintare Pylint directives and are kept ascode cells.Shebang is considered as acode cell, eg.#!/usr/bin/env python3.Encodings like# -*- coding: utf-8 -*-are also considered ascode cells.Feedback and pull requestsIf you do like this, star me maybe? Pull requests are very much encouraged! Slide into my DM with suggestions too!