Skip to main content

Create Markdown documents from within your Python code.

Project description

Markdown Writer

Create Markdown documents from within your Python code.

I recently worked on a data science project in which I had to generate a Markdown report to display interim results from within my Python script. I was unsatisfied by how clunky is felt, particulary to write things like lists. For example, if you want to output a list of bullets from a list, you would have to do something like this:

output = ''
for item in my_list:
    output += f"- {item}\n"

# OR
output = "\n".join([f"- {item}" for item in my_list])

It gets more complicated (and ugly) very quickly when writing nested lists, and even keeping track of the required number of newline characters takes more effort than it should for such a simple task.

The purpose of this library is to create simple abstractions to make this simple task effortless. To turn your Python list into a Markdown list, simply call md.list(my_list).

# Instantiate MarkdownWriter() at the top of your script
md = MarkdownWriter()
# And then build your output step-by-step
md.list(my_list)

See ./examples/example.py for more examples.

Features

  • Headings
  • Lists
    • Numbered lists
    • Nested lists
  • Code blocks
  • Tables from Pandas DataFrames
  • Hyperlinks
  • Images
  • Saving to File

Installation

Install with pip:

pip install python-markdown-writer

Example usage

Code Block Example

md.code_block('Hello, World!')

Tables from Pandas

df = pd.DataFrame({
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [25, 30, 35],
    "City": ["Cape Town", "Johannesburg", "Durban"]
})
md.table_from_pandas(df)
Name Age City
Alice 25 Cape Town
Bob 30 Johannesburg
Charlie 35 Durban

List Example

# This is how you create a list:
sports = ["Rugby", "Football", "Cricket"]
md.list(sports)
  • Rugby
  • Football
  • Cricket

Nested List Example

Using list levels

A simple way to create nested lists is by specifying the list level.

# This is how you create a nested list:
md.list(["Sports"], level=1)
md.list(["Rugby", "Football", "Cricket"], level=2)
  • Sports

    • Rugby
    • Football
    • Cricket

Using a dictionary

You can also create nested lists from a dictionary.

# This is how you create a nested list from a dictionary:
produce = {
    "Fruits": {
        "Citrus": ["Orange", "Lemon", "Lime"],
        "Berries": ["Strawberry", "Blueberry", "Raspberry"]
    },
    "Vegetables": {
        "Leafy": ["Spinach", "Lettuce"],
        "Root": ["Carrot", "Beetroot"]
    }
}
md.nested_list_from_dict(produce)
  • Fruits
    • Citrus
      • Orange
      • Lemon
      • Lime
    • Berries
      • Strawberry
      • Blueberry
      • Raspberry
  • Vegetables
    • Leafy
      • Spinach
      • Lettuce
    • Root
      • Carrot
      • Beetroot

Numbered List Example

# This is how you create a numbered list:
sports = ["Rugby", "Football", "Cricket"]
md.numbered_list(sports)
  1. Rugby
  2. Football
  3. Cricket

Image Example

Cape Town is a spectacular city.

md.image(alt_text='Cape Town', url=url)

Cape Town

Project details


Download files

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

Source Distribution

python_markdown_writer-0.2.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

python_markdown_writer-0.2.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file python_markdown_writer-0.2.0.tar.gz.

File metadata

File hashes

Hashes for python_markdown_writer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 660e9f28efdab472990953523fd2092296bda50e97c8668f7a4ff41472c20f7f
MD5 7e09fda952a8574e6f3a7be171513508
BLAKE2b-256 57d3712d707d029eb64926703ba9b9265b5b133624426d6e2bca1fcd1c311d13

See more details on using hashes here.

File details

Details for the file python_markdown_writer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_markdown_writer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1f58949e125c0594f6f1cb072c21391bc57fccb78d26b752665397ef0511d92
MD5 17428346b9ad56cd6e64126e68e520e2
BLAKE2b-256 02327d2f9e7a4ede2f31d13907c5c7e029ca6b909724bb4c422225884f5a2435

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 Pingdom Monitoring Sentry Error logging StatusPage Status page