I use a package eyeseast/python-frontmatter to load files with frontmatter in them. Its a handy package that allows you to load files with structured frontmatter (yaml, json, or toml).
Install ยถ #
Itโs on pypi, so you can install it into your virtual environment with pip.
python -m pip install python-frontmatter
๐ Whatโs Frontmatter ยถ #
Frontmatter is a handy way to add metadata to your plain text files. Itโs quite common to have yaml frontmatter in markdown. All of my blog posts have yaml frontmatter to give the post metadata such as post date, tags, title, and template. dev.to is a popular developer blogging platform that also builds all of its posts with markdown and yaml frontmatter.
Letโs see an example ยถ #
Here is the exact frontmatter for this post you are reading on my site.
---
date: 2022-03-24 03:18:48.631729
templateKey: til
title: How I load Markdown in Python
tags:
- linux
- python
---
This is where the markdown content for the post goes.
So itโs yaml ยถ #
yaml is the most commmon, but python-frontmatterHandlers for toml and json. also supports
If you want a good set of examples of yaml learnxinyminutes has a fantastic set of examples in one page.
How to load yaml frontmatter in python ยถ #
Here is how I would load this post into python using python-frontmatter.
import frontmatter
inspect(frontmatter.load("pages/til/python-frontmatter.md"))
We can use rich to inspect the Post object to see what all it contains.
โฏ inspect(frontmatter.load("pages/til/python-frontmatter.md"))
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'frontmatter.Post'> โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ A post contains content and metadata from Front Matter. This is what gets โ
โ returned by :py:func:`load <frontmatter.load>` and :py:func:`loads <frontmatter.loads>`. โ
โ Passing this to :py:func:`dump <frontmatter.dump>` or :py:func:`dumps <frontmatter.dumps>` โ
โ will turn it back into text. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ <frontmatter.Post object at 0x7f03c4c23ca0> โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ content = "I use a package\n[eyeseast/python-frontmatter](https://github.com/eyeseast/python-frontmatter)\nto load files with frontmatter in โ
โ them. Its a handy package that allows you to\nload files with structured frontmatter (yaml, json, or toml).\n\n## Install\n\nIt's โ
โ on pypi, so you can install it into your virtual environment with pip.\n\n```bash\npython -m pip install โ
โ python-frontmatter\n```\n\n## ๐ What's Frontmatter\n\nFrontmatter is a handy way to add metadata to your plain text files. โ
โ It's\nquite common to have yaml frontmatter in markdown. All of my blog posts have\nyaml frontmatter to give the post metadata such โ
โ as post date, tags, title, and\ntemplate. dev.to is a popular developer blogging platform that also builds all\nof its posts with โ
โ markdown and yaml frontmatter.\n\n## Let's see an example\n\nHere is the exact frontmatter for this post you are reading on my โ
โ site.\n\n```markdown\n---\ndate: 2022-03-24 03:18:48.631729\ntemplateKey: til\ntitle: How I load Markdown in Python\ntags:\n - โ
โ linux\n - python\n\n---\n\nThis is where the markdown content for the post goes.\n```\n\n## So it's yaml\n\nyaml is the most โ
โ commmon, but\n[eyeseast/python-frontmatter](https://github.com/eyeseast/python-frontmatter)\nalso โ
โ supports\n[Handlers](https://python-frontmatter.readthedocs.io/en/latest/handlers.html?highlight=toml#module-frontmatter.default_haโฆ โ
โ toml and json.\n\nIf you want a good set of examples of yaml\n[learnxinyminutes](https://learnxinyminutes.com/docs/yaml/) has a โ
โ fantastic set\nof examples in one page.\n\n## How to load yaml frontmatter in python" โ
โ handler = <frontmatter.default_handlers.YAMLHandler object at 0x7f03bffbd910> โ
โ metadata = { โ
โ 'date': datetime.datetime(2022, 3, 24, 3, 18, 48, 631729), โ
โ 'templateKey': 'til', โ
โ 'title': 'How I load Markdown in Python', โ
โ 'tags': ['linux', 'python', 'python'] โ
โ } โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Getting Metadata ยถ #
You can get items from the posts metadata just as you would from a dict.
post = frontmatter.load("pages/til/python-frontmatter.md")
post['date']
# datetime.datetime(2022, 3, 24, 3, 18, 48, 631729)
post.get('date')
# datetime.datetime(2022, 3, 24, 3, 18, 48, 631729)
I have recently become fond of the
.getmethod to give it an easy default value.
Content is content ยถ #
The content of the document is stored under .content
post.content