Skip to main content

A wrapper of mistletoe to output Markdown as Dash components

Project description

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')

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

Dash mistletoe-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distributions

dash_mistletoe-0.1.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

Dash_mistletoe-0.1.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file Dash mistletoe-0.1.0.tar.gz.

File metadata

  • Download URL: Dash mistletoe-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for Dash mistletoe-0.1.0.tar.gz
Algorithm Hash digest
SHA256 85ad3cfe8e81b1c2b1cdc33bdee8807ad9b13ac9ec30204611f9566a42f65e1b
MD5 a6a1a73b7fb4e5d4cfa04302a68c8e97
BLAKE2b-256 6bd17dc786af01f355568b3caf6275f9121d4131482caa5df538833d7cf0037d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dash_mistletoe-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 248e550fbc7f35001429d2113c821e4d1cde751c372176fbf087a10b60f8aa1a
MD5 3f289390f561b5967a89b45d212ea0b9
BLAKE2b-256 06fbc0b44c84a97bb80ba1ec471ccceba7878ed0f5ba7404d28f9b59d62a765c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for Dash_mistletoe-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e754eec1f048e3f51ebd80f28108dd764d340865837538fc16a0071074068d0
MD5 c713606987ea4ceb53385f1474ecf9df
BLAKE2b-256 06e48263adaeaad6278aed25bc017a7388fcf6006e4d8665324659dcc68b5082

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page