Skip to main content

[Jekyll](http://jekyllrb.com/)-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](https://travis-ci.org/eyeseast/python-frontmatter.svg?branch=master)](https://travis-ci.org/eyeseast/python-frontmatter)

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.4.4.tar.gz (8.8 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.4.4-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python-frontmatter-0.4.4.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.5

File hashes

Hashes for python-frontmatter-0.4.4.tar.gz
Algorithm Hash digest
SHA256 222203a9f56469c564b97b2d2607fde898b8546802d8361e729f087490761eff
MD5 25947bd4d3d760cbb33cd2ea82dce487
BLAKE2b-256 888f731d5a36b087646c295274be795161990ca7df5dbf7fbb125494ae1290b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_frontmatter-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.5

File hashes

Hashes for python_frontmatter-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 94fc9ca449eddaae281455f2ba15662150c81d1e69c2f08a77ad11a5f38dfa1d
MD5 7cd5f457cde21245ddc3201abf61192f
BLAKE2b-256 6a1fe5ee1d4f942441ecf512544ccfcb4c194fadff7289f8f6d41ef52f522371

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