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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for python_markdown_writer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a05c457013634f015e3e8fd401b9c00eeeddf91f36ebecd4be27c711c590245c
MD5 e9183c10b99b6e99eddd729bba137072
BLAKE2b-256 0b4a97cc81f192219536780b4cc1ef909dddb106555d9341cc57987f29418fcd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_markdown_writer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f0f8617a9817944675287f56458c3b18b7c1312b49f1b8e5530aa09d2d2ccc6
MD5 55bbaaffdbc1ab3c9b036e2311146d1e
BLAKE2b-256 9e2cca4daf807c6babc0625018d4f42426bcdd8e87a01f43b46897e09f1d9ebf

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