Skip to main content

Python Frontmatter

Jekyll-style YAML front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.

This is a small package to load and parse files (or just text) with YAML front matter.

Build Status

Install:

pip install python-frontmatter

Usage:

>>> import frontmatter

Load a post from a filename:

>>> post = frontmatter.load('tests/hello-world.markdown')

Or a file (or file-like object):

>>> with open('tests/hello-world.markdown') as f:
...     post = frontmatter.load(f)

Or load from text:

>>> with open('tests/hello-world.markdown') as f:
...     post = frontmatter.loads(f.read())

Access content:

>>> print(post.content)
Well, hello there, world.

# this works, too
>>> print(post)
Well, hello there, world.

Use metadata (metadata gets proxied as post keys):

>>> print(post['title'])
Hello, world!

Metadata is a dictionary, with some handy proxies:

>>> sorted(post.keys())
['layout', 'title']

>>> from pprint import pprint
>>> post['excerpt'] = 'tl;dr'
>>> pprint(post.metadata)
{'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}

If you don't need the whole post object, just parse:

>>> with open('tests/hello-world.markdown') as f:
...     metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!

Write back to plain text, too:

>>> print(frontmatter.dumps(post)) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.

Or write to a file (or file-like object):

>>> from io import BytesIO
>>> f = BytesIO()
>>> frontmatter.dump(post, f)
>>> print(f.getvalue().decode('utf-8')) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-frontmatter-0.5.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_frontmatter-0.5.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file python-frontmatter-0.5.0.tar.gz.

File metadata

  • Download URL: python-frontmatter-0.5.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5

File hashes

Hashes for python-frontmatter-0.5.0.tar.gz
Algorithm Hash digest
SHA256 a9c2e90fc38e9f0c68d8b82299040f331ca3b8525ac7fa5f6beffef52b26c426
MD5 3b2acbcc68646e9e9868f3b10f7e6782
BLAKE2b-256 36bfa3ecd21cb594b307e39500a1cfadbde5635d904b87faf0196cdc0214eeb3

See more details on using hashes here.

File details

Details for the file python_frontmatter-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: python_frontmatter-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5

File hashes

Hashes for python_frontmatter-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7dcdfdaf498d488dce98bfa9452f8b70f803a923760ceab1ebd99291d98d28a
MD5 1fe12fbedb83f74682d36753b4ba0ccd
BLAKE2b-256 5b4b3dbb864fb6dd419366de3f4c2bb3178d239d6d3e0bb7cd938f8edeed5820

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page