Frontmatter Parsing With Python
Tags: beginners, python, 100daysofpython
Published: 2021-08-23 10:15:12

_**I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to [visit my blog](https://blog.dennisokeeffe.com) and subscribe to my newsletter. [Follow me on Twitter](https://twitter.com/dennisokeeffe92).**_

> This is Day 4 of the #100DaysOfPython challenge.

This post will use the [python-frontmatter](https://pypi.org/project/python-frontmatter/) library to parse an example markdown file with [frontmatter](https://www.gatsbyjs.com/docs/mdx/writing-pages/#using-frontmatter-in-mdx) to demonstrate how to parse metadata from your markdown files.

This can be a useful tool to colocate important information within your markdown that can be access when programmatically reading files!

## Prerequisites

1. Familiarity with [Pipenv](https://pipenv.pypa.io/en/latest/). See [here](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv) for my post on Pipenv.
2. Familiarity with [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/gettingstarted/install.html). See [here](https://blog.dennisokeeffe.com/blog/2021-07-19-hello-jupyter-notebook) for my post on JupyterLab.
3. [Frontmatter in MDX](https://www.gatsbyjs.com/docs/mdx/writing-pages/#using-frontmatter-in-mdx)

## Getting started

Let's create the `hello-frontmatter` directory and install `python-frontmatter`. We will also need to add an example markdown file.

```s
# Make the `hello-frontmatter` directory
$ mkdir hello-frontmatter
$ cd hello-frontmatter
# Create an example MDX file
$ touch frontmatter-example.mdx

# Init the virtual environment
$ pipenv --three
$ pipenv install python-frontmatter
$ pipenv install --dev jupyterlab
```

Inside of `frontmatter-example.mdx` add the following:

```mdx
---
title: Henlo, FrontMatter
date: "2016-12-16"
---

Henlo world, this is me.
```

At this stage, we are ready to parse the file and get the metadata.

Start up the notebook server:

```s
# Startup the notebook server
$ pipenv run jupyter-lab
# ... Server is now running on http://localhost:8888/lab
```

The server will now be up and running.

## Creating the notebook

Once on http://localhost:8888/lab, select to create a new Python 3 notebook from the launcher.

Ensure that this notebook is saved in `hello-frontmatter/docs/<your-file-name>`.

We will create three cells to handle each part of this project:

1. Import the `python-frontmatter` library and related modules for the `os.path` library to help determine the relative path.
2. Use the `os.path` imports to get the relative path to the file.
3. Load the frontmatter metadata from the file and print them to the console.

## Importing the required libraries

```py
import frontmatter
from os.path import join, dirname, abspath
```

## Get the relative path

```py
mdx_filepath = join(dirname(abspath("__file__")), '../frontmatter-example.mdx')
print(mdx_filepath)
# ... prints out path to the markdown file
```

## Load the frontmatter metadata

Finally, we can use the `frontmatter.load` method to parse the frontmatter metadata from the file.

```py
post = frontmatter.load(mdx_filepath)
print(post.keys())
print(post['title']) # prints "Henlo, FrontMatter"
print(post['date']) # prints "2016-12-16"
```

## Resources and further reading

1. [The ABCs of Pipenv](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv) for the minimum you will need.
2. [Hello, JupyterLab](https://blog.dennisokeeffe.com/blog/2021-07-19-hello-jupyter-notebook).
3. [Frontmatter in MDX](https://www.gatsbyjs.com/docs/mdx/writing-pages/#using-frontmatter-in-mdx)
4. [python-frontmatter library](https://pypi.org/project/python-frontmatter/)


_Originally posted on my [blog](https://blog.dennisokeeffe.com/blog/2021-07-23-frontmatter-parsing-with-python). To see new posts without delay, read the posts there and subscribe to my newsletter._