How to use notedown to complete the format conversion between ipynb and markdown?

Posted by bampot on Sat, 15 Jan 2022 05:08:56 +0100

Introduction: use Convert IPython Notebooks to markdown (and back) downnote in can easily convert IPYNB to Markdown format. This paper introduces and tests the use of notedown software. However, if you use notedown to convert markdowns from CSDN, you need to filter the number of more than markdowns in CSDN.

Keywords: notedown, ipynb, markdown

 

§ 01 notedown software

  on Github Convert IPython Notebooks to markdown (and back) except notedown The software package is used to Ipython notebook Tool for converting with Markdown documents.

1.1 installation and use

1.1.1 installing notedown

  • Direct installation:
pip install notedown
  • Install the latest version of Github:
pip install https://github.com/aaren/notedown/tarball/master

1.1.2 notedown application

  basic commands for application:

(1)Markdown→IPYNB

notedown input.md > output.ipynb

(2)IPYNB→Markdown

 Ⅰ. Remove output

  remove all the outputs in IPYNB and store the rest in Markdown document:

notedown input.ipynb --to markdown --strip > output.md
 Ⅱ. With output

    the following is to store all the output in the notebook document in the output JSON format without changing:

notedown input.ipynb --to markdown > output_with_outputs.md

  the reason why the output of the Notebook is saved in the JSON format in Markdown is to facilitate the conversion of the document to the Notebook format in the future. That is, you can edit the Markdown document and convert it to IPYNB format. After Jupiter Notebook plays with IPYNB files a little, it can also be converted into Markdown documents again.

  the following is to remove the JSON format output content in Markdown.

notedown with_output_cells.md --to markdown --strip > no_output_cells.md

1.1.3 running ippython

notedown notebook.md --run > executed_notebook.ipynb

  for other notedown features, see Original document on Github All right.

1.2 initial operation notedown

  you can directly run the notedown command in the Windows command window.

▲ figure 1.2.1 run Notedown in Cmd command window of Windows

  run the output corresponding to notedown.

usage: notedown [-h] [-o [OUTPUT]] [--from {notebook,markdown}]
                [--to {notebook,markdown}] [--run] [--timeout TIMEOUT]
                [--strip] [--precode PRECODE [PRECODE ...]] [--knit [KNIT]]
                [--rmagic] [--nomagic] [--render] [--template TEMPLATE]
                [--match MATCH] [--examples] [--version] [--debug]
                [input_file]

Create an IPython notebook from markdown.

positional arguments:
  input_file            markdown input file (default STDIN)

optional arguments:
  -h, --help            show this help message and exit
  -o [OUTPUT], --output [OUTPUT]
                        output file, (default STDOUT). If flag used but no
                        file given, use the name of the input file to
                        determine the output filename. This will OVERWRITE if
                        input and output formats are the same.
  --from {notebook,markdown}
                        format to convert from, defaults to markdown or file
                        extension
  --to {notebook,markdown}
                        format to convert to, defaults to notebook or file
                        extension. Setting --render forces this to 'markdown'
  --run, --execute      run the notebook, executing the contents of each cell
  --timeout TIMEOUT     set the cell execution timeout (in seconds)
  --strip               strip output cells
  --precode PRECODE [PRECODE ...]
                        additional code to place at the start of the notebook,
                        e.g. --pre '%matplotlib inline' 'import numpy as np'
  --knit [KNIT]         pre-process the markdown with knitr. Default chunk
                        options are 'eval=FALSE' but you can change this by
                        passing a string. Requires R in your path and knitr
                        installed.
  --rmagic              autoload the rmagic extension. Synonym for --precode
                        '%load_ext rpy2.ipython'
  --nomagic             disable code magic.
  --render              render outputs, forcing markdown output
  --template TEMPLATE   template file
  --match MATCH         determine kind of code blocks that get converted into
                        code cells. choose from 'all' (default), 'fenced',
                        'strict' or a specific language to match on
  --examples            show example usage
  --version             print version number
  --debug               show logging output

Example: notedown some_markdown.md > new_notebook.ipynb

 

§ 02 test example

  the following tests the effect of notedown software conversion.

2.1 IPYNB→Markdown

  in How to use nbconvert to convert IPYNB documents into Markdown documents? In, nbconvert is used to convert ipynb documents into markdown, but nbconvert will no longer support ipython in the future, so notedown can be used to complete document conversion.

2.1.1 converting documents

  IPYNB documents are from Propeller AI Studio - artificial intelligence learning and training community Medium Label generation based on self supervised learning target detection Main Ipynb documentation.

notedown main.ipynb --to markdown >main1.md

   main1.0 is generated in this directory md.

(1) File format problem

   when importing the above documents through the Markdown of CSDN, you can see that there are some problems, mainly the problem of image reference. This is a problem that can't be done in CSDN.

▲ there is a problem with the picture reference in Figure 2.1.1

2.2 Markdown→IPYNB

  command format:

notedown main.md >main11.ipynb

2.2.1 converting documents from IPYNB

  the following is to re convert the Markdown generated by the nbconvert conversion of IPYNB documents into Markdown documents.

   transform to generate the corresponding main11 ipynb.

  set main11 When the ipynb document is imported into the BML Codelab environment, you can see that the display is correct.

▲ figure 2.2.1 import IPYNB documents into BML CodeLab

2.2.2 converting documents from CSDN

  convert documents and select blog posts on CSDN Test the problem of the recorded telephone dialing sound signal during transmission , use the export of CSDN, export it as "phone.md", and then use notedown to convert it.

notedown phone.md >phone.ipynb

  phone to be generated Ipynb is imported into BML Codelab and opened with its Notebook. You can see:

  • Code Cell and Markdown text can be well separated, including the format modification in the original Markdown;
  • Error: the original picture size under CSDN cannot be adapted to the picture size in the Notebook;

▲ figure 2.2.2 viewing CSDN conversion IPYNB document in notebook

  therefore, it is necessary to filter more modified formats in CSDN, and then use notedown for conversion.

 

※ general ※ conclusion ※

  use Convert IPython Notebooks to markdown (and back) downnote in can easily convert IPYNB to Markdown format. This paper introduces and tests the use of notedown software.

  however, if you use notedown to convert markdowns from CSDN, you need to filter the number of more than markdowns in CSDN.

■ links to relevant literature:

● relevant chart links:

Topics: github markdown