Jupyter Notebook It's convenient, do you guys use it? It's convenient for implementing and checking the code, but if you devise it, you can even do unit tests and manual production. In this article, I will introduce a convenient usage that is a little different from the conventional usage.
Jupyter is convenient because you can write code while checking the result. Wouldn't it be convenient if the confirmation work could be used for unit testing as it is?
You can do it with pytest-ipynb!
pytest-ipynb
can be easily installed with pip
.
$ pip install pytest-ipynb
This module is an extension of the test library py.test, so you also need to install py.test.
It's easy to use, just throw a Notebook named test * .ipynb
into the directory where you're testing.
After that, you can write the test contents in that cell as follows.
Test example
''' mul test '''
a = 100 * 2
assert a == 200
I tried to translate the function list of the manual.
--Discover the'test * .ipynb' file -Each cell in Notebook runs as a unit test (just use assert) --The comment (docstring) in the first row of each cell is treated as a test name or function name. --If you put'fixture *'or'setup *'in the docstring, it will be executed before each unit test. --If you write'SKIPCI'in the docstring, the Travis-CI test will be skipped (check if the CI environment variable is defined). --The IPython Notebook kernel will be restarted after testing --The working folder during testing is local to the .ipynb file
If you make a mistake in the translation, please point it out.
After that, just run py.test -v
normally, and it will do the test including Notebook.
It can also be used for Readme.md on Github, which is convenient when you want to describe the execution result. GitHub of the module "Image4Layer" that I created also generates Readme.md from Jupyter.
https://github.com/pashango2/Image4Layer
Choose File> Download as> Markdown (.md) from the Jupyter menu to generate a markdown zip file.
Rename the generated .md and tweak it and you're done. However, if you generate an image etc., the image will be expanded in the root directory, so I think that it needs to be corrected.
As I wrote in the article "I want everyone to use Sphinx conveniently" (http://qiita.com/pashango2/items/d1b379b699af85b529ce), Notebook can be added to Sphinx as well. I will. There is an extension of Sphinx called nbsphinx that allows you to capture Jupyter notebooks.
Installation is easy with pip
.
pip install nbsphinx
When imported into Sphinx, the display will be as follows.
For detailed settings of Sphinx, refer to the following article.
I made "sphinx-quickstart-plus" to make Sphinx documentation convenient I want everyone to use Sphinx conveniently
It's a basic thing, but I think some people may not know it, so I will describe it.
The % timeit
command also measures the processing time of a function with Jupyter.
Moreover, the best time is measured with an appropriate number of trials in consideration of the fluctuation of the processing time, so you can get higher quality measurement results than you can measure by yourself.
In [1]: %timeit l = [x ** 2 for x in range(1000)]
1000 loops, best of 3: 45 ms per loop
I want to read other people's Jupyter Notebook utilization articles!
Recommended Posts