Frontmatter Parsing With Python
Published: Jul 23, 2021
Last updated: Jul 23, 2021
This is Day 4 of the #100DaysOfPython challenge.
This post will use the python-frontmatter
This can be a useful tool to colocate important information within your markdown that can be access when programmatically reading files!
Prerequisites
- Familiarity with Pipenv
. See here for my post on Pipenv. - Familiarity with JupyterLab
. See here for my post on JupyterLab. - 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.
# 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:
--- 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:
# 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:
- Import the
python-frontmatter
library and related modules for theos.path
library to help determine the relative path. - Use the
os.path
imports to get the relative path to the file. - Load the frontmatter metadata from the file and print them to the console.
Importing the required libraries
import frontmatter from os.path import join, dirname, abspath
Get the relative path
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.
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
- The ABCs of Pipenv
for the minimum you will need. - Hello, JupyterLab
. - Frontmatter in MDX
- python-frontmatter library
Dennis O'Keeffe
Melbourne, Australia
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.
Frontmatter Parsing With Python
Introduction