Skip to main content

dash_mistletoe

An extension of mistletoe that converts markdown into dash components.

Purpose

While other Markdown parsers exist, they lack the ability to customize items at the component level. This creates a barrier when we want to change styles or add ids to elements. dash_mistletoe fixes this problem by providing a baseline framework for extending Markdown using dash.

We build an extension of mistletoe; a fast, spec-compliant, and customizable Markdown parser; for dash components. mistletoe parses to an Abstract Syntax Tree allowing us to extend easily.

Install

To install and use, run

pip install dash-mistletoe

To clone and run locally, run

git clone https://github.com/bradley-erickson/dash-mistletoe
pip install -r requirements.txt
python usage.py

Usage

First, mistletoe will parse markdown_to_render using the DashRenderer. The output will contain all contents of the markdown wrapped in a Div.

from dash import html
from dash_mistletoe.dash_renderer import DashRenderer
import mistletoe

markdown_to_render = """
# Heading level 1

I like markdown
"""

component = mistletoe.markdown(markdown_to_render, DashRenderer)
component
# html.Div([html.H1('Heading level 1'), html.P('I like markdown')])

Extending Components

To extend a component, we will first make a sub class of DashRenderer to inherit all methods. Next, we just need overwrite the methods we want to customize. The possible methods to overwrite can be referenced in the code.

class MyDashRenderer(DashRenderer):

    def render_heading(self, token):

Depending on what we need done with the content, we can create the component with the super class first. Then, we change an attribute about it. In the heading example, I add an id to each heading element. Doing this allows for automatic scroll when changing the hash of the URL. This is great for adding a table of contents to a blog post.

class MyDashRenderer(DashRenderer):

    def render_heading(self, token):
        # fetch the super class heading first i.e. an html.Hx component
        heading = super().render_heading(token)
        # render the child (token) to as text (plain)
        id = self.render_to_plain(token).lower().replace(' ', '')
        # set the id and return
        heading.id = id
        return heading

You can also completely ignore the super class method and return something brand new. For example, we can return a P element with a specific className instead of Strong one. This allows us to handle how the Strong text looks with CSS instead.

class MyDashRenderer(DashRenderer):

    def render_strong(self, token):
        # render the children (token)
        children = self.render_inner(token)
        return html.P(children, className='strong-text')

Metadata

Release files for dash-mistletoe 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dash-mistletoe 0.2.4
File Size Uploaded Provenance
dash_mistletoe-0.2.4.tar.gz 5.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for dash-mistletoe 0.2.4
File Interpreter ABI Platform
dash_mistletoe-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / dash_mistletoe-0.2.4.tar.gz

Download URL dash_mistletoe-0.2.4.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
25a8f45ca1714311e8c3df483374528b20b28fdbb2726ea90d3397e2ecf26ba1
BLAKE2b-256 checksum
How to use checksums
9e7450699b4a243d4fe21ebf693f9e474e6d14d82fae56536e4653765bfc5836
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release files / dash_mistletoe-0.2.4-py3-none-any.whl

Download URL dash_mistletoe-0.2.4-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3357c6e64810ad441f20b7eb39d988fcc5927e761c696f733aeb10be85a49ce6
BLAKE2b-256 checksum
How to use checksums
d11ea3b839057bcdb64738309cd56a7ebaa34a180b17cc8118f9d38b7066a902
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Security for dash-mistletoe 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page