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.1.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.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for python_markdown_writer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 675c01bbc11d0ae3024565beefbec031156d92ddb7f758fa78eac2caaaedae6e
MD5 6fae6801cbc8a65ced601d80292e1d65
BLAKE2b-256 64f7b9909f1bdd517a7f359e936d3c592c009a0fe4cff4b0f352d0b56a859587

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_markdown_writer-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27d2c4b6c0ecda99fd60d3b2051c56b4f98edfeccb0da8f0fb373961b2deb5ee
MD5 661fa114c6b1549c59026662f50af903
BLAKE2b-256 5723e5b871c7c2411f65ced0f39d44403db6064ad8a7e8c7337c41697b852e9f

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