# Converting Formats (ipynb, md)

(converting/ipynb-md)=
## Jupyter Notebook (ipynb) to Myst Markdown (myst)

It is recommended to use [jupytext](https://jupytext.readthedocs.io/en/latest/) to
convert `ipynb` to `md` (MyST) files. This `tool` is included in the [quantecon environment](https://quantecon.github.io/QuantEcon.manual/intro.html#the-quantecon-environment).

Once it is installed you can do this via the [command line](https://jupytext.readthedocs.io/en/latest/using-cli.html#command-line-conversion)

```bash
jupytext <notebook>.ipynb --to myst
```

Or you can use the [Jupyter menu and pair the notebook](https://jupytext.readthedocs.io/en/latest/install.html#jupytext-menu-in-jupyter-notebook) with a `Myst Markdown` file for automatic file creation using `jupyter`.

````{warning}
When you convert the `ipynb` file to a `myst markdown` file using `jupytext` you will get two files,
the original `ipynb` and the new `md` file. If you build using `jupyter-book` it will issue a
warning that there are two source files such as:

```bash
WARNING: multiple files found for the document "BCG_complete_mkts": ['BCG_complete_mkts.ipynb', 'BCG_complete_mkts.md']
Use 'python-lecture-sandpit.myst/lectures-mixed/BCG_complete_mkts.md' for the build.
```

By **default** it will use the `md` version of the file to build the project.

````

(converting/md-ipynb)=
## Myst Markdown (md) to Jupyter Notebook (ipynb)

It is recommended to use [jupytext](https://jupytext.readthedocs.io/en/latest/) to
convert `md` (MyST) to `ipynb` files. This `tool` is included in the [quantecon environment](https://quantecon.github.io/QuantEcon.manual/intro.html#the-quantecon-environment).

Once it is installed you can do this via the [command line](https://jupytext.readthedocs.io/en/latest/using-cli.html#command-line-conversion)

```bash
jupytext <filename>.md --to ipynb
```

:::{tip}
If you are starting from `markdown` you will need to let jupytext know it is encoded as a myst document.
The easiest way to do this is to add header metadata to the top of your markdown document. 

For a python project it would be:

```yaml
---
jupytext:
  text_representation:
    extension: .md
    format_name: myst
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---
```
:::

If `jupytext` is installed then you can also open the `md` file directly with `jupyter notebook` and it will
convert the file to `ipynb` when it opens the `md` file.

````{warning}
When you convert the `myst markdown` file to an `ipynb` file using `jupytext` you will get two files,
the original `md` file and the new `ipynb` file. If you build using `jupyter-book` it will issue a
warning that there are two source files such as:

```bash
WARNING: multiple files found for the document "BCG_complete_mkts": ['BCG_complete_mkts.ipynb', 'BCG_complete_mkts.md']
Use 'python-lecture-sandpit.myst/lectures-mixed/BCG_complete_mkts.md' for the build.
```

By **default** it will use the `md` version of the file to build the project.

````