[PYTHON] How to set Jupytext nicely when managing code as a team

It's painful to let someone else edit ~ / .jupyter / jupyter_notebook_config.py with jupytext's automatic conversion settings. I have a good setting method so I will share it.

First conclusion

Place the file with the following contents in the root of the project.

jupytext.yml


default_jupytext_formats: "ipynb,py:percent"

What is jupytext

jupytext is a tool to convert .ipynb files to .py, .R, .jl, .md, .Rmd and so on. By setting it, it works with jupyter lab and automatically converts it when saving a file. It can also be executed from the command line, so it is versatile.

There is another tool for converting to .ipynb-> .py, nbconvert, but it requires a lot of ingenuity to convert automatically when saving.

.ipynb <-> .py conversion

Analyzing with multiple people will reveal the bad points of .ipynb. There are some

--It's heavy to manage git. --It is in json format and difficult to read at the time of review.

It is difficult to share. One solution to this is to convert it to python format. This makes the notebook lightweight and easy to read, which is a nice touch. You can also reproduce the notebook without any problem by converting .py-> .ipynb.

environment

#Pipenv environment
python 3.8.5
jupyterlab 2.2.6

Usage procedure

1. Install jupytext

If you are not using pipenv, replace it with a good one.

$ pipenv install jupytext

2. Build jupyter lab

When you start jupyter lab after installation, you will be asked if you want to build, so please build as it is. If you want to build manually, use pipenv run jupyter lab build.

How to set

4 types of setting methods

There are four setting methods.

  1. Edit the .ipynb metadata
  2. jupytext --set-formats ipynb,py notebook.ipynb
  3. Edit ~ / .jupyter / jupyter_notebook_config.py
  4. Create a jupytext configuration file

1 and 2 need to be set for each file, which is troublesome. Operation cannot be guaranteed for 3 because it is necessary to operate files outside the repository.

This time, I will explain about No. 4. This way it's kind because it's already set up when someone pulls the repository.

Creating a configuration file

Create a configuration file with one of the following names. .jupytext jupytext.toml jupytext.yml jupytext.yaml jupytext.json jupytext.py Or the format with . added to the beginning of these

If you put this file in the root of the project, it will be read automatically. Please check the reference link for details on the location.

Automatic conversion will be effective by writing as follows. This time I wrote it in yaml.

jupytext.yml


default_jupytext_formats: "ipynb,py:percent"

Considering the use with VS Code and PyCharm, the py: percent format is recommended for python files.

As a result, I think the directory structure will look like this.

.
├── Pipfile
├── Pipfile.lock
├── notebook.ipynb  #Created notebook
├── notebook.py  #Converted and generated python
└── jupytext.yml  #setting file

Summary

-- .ipynb <-> .py conversion makes it easier for teams to manage code. --Jupytext can be automatically converted when saving a file. --There are various ways to set Jupytext --Easy to share settings with configuration file --py: percent is safe

that's all!

reference

Configuration — Jupytext documentation

Recommended Posts

How to set Jupytext nicely when managing code as a team
Qiita (1) How to write a code name
How to disguise a ZIP file as a PNG file
How to code a drone using image recognition
How to remember when you forget a word
How to set up a local development server
[Linux] [kernel module] How to pass parameters as arguments when loading a loadable kernel module
How to manage arguments when implementing a Python script as a command line tool
How to set up a Python environment using pyenv
How to display DataFrame as a table in Markdown
How to make a Python package using VS Code
How to create a kubernetes pod from python code
How to print characters as a table with Python's print function
How to use cuML SVC as a Gridsearch CV classifier
Be careful when assigning Series as a column to pandas.DataFrame
How to register a package on PyPI (as of September 2017)
How to use a file other than .fabricrc as a configuration file
How to import NoteBook as a module in Jupyter (IPython)
How doi may be useful when asking how to write code?
Set VS Code to PyCharm.
How to hack a terminal
How to put the string JSON passed as a parameter into the dictionary when writing Ansible module
How to set a shortcut to switch full-width and half-width with IBus
[Python] How to store a csv file as one-dimensional array data
How to deploy a web application on Alibaba Cloud as a freelancer
How to get a job as an engineer from your 30s
How to make a Japanese-English translation
How to write a Python class
How to put a symbolic link
How to make a slack bot
How to create a Conda package
Code to randomly generate a score
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
How to add sudo when debugging
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
How to delete a Docker container
How to make a crawler --Basic
How to run TensorFlow 1.0 code in 2.0
How to create a config file
[Python] What is a formal argument? How to set the initial value
How to install python package in local environment as a general user
How to set up a jupyter notebook on ssh destination (AWS EC2)
How to write a string when there are multiple lines in python
[Development environment] How to create a data set close to the production DB