Markdown¶
Added in version 0.11.0
A widget to display a Markdown document.
- Focusable
- Container
Tip
See MarkdownViewer for a widget that adds additional features such as a Table of Contents.
Example¶
The following example displays Markdown from a string.
from textual.app import App, ComposeResult
from textual.widgets import Markdown
EXAMPLE_MARKDOWN = """\
## Markdown
- Typography *emphasis*, **strong**, `inline code` etc.
- Headers
- Lists
- Syntax highlighted code blocks
- Tables and more
## Quotes
> I must not fear.
> > Fear is the mind-killer.
> > Fear is the little-death that brings total obliteration.
> > I will face my fear.
> > > I will permit it to pass over me and through me.
> > > And when it has gone past, I will turn the inner eye to see its path.
> > > Where the fear has gone there will be nothing. Only I will remain.
## Tables
| Name | Type | Default | Description |
| --------------- | ------ | ------- | ---------------------------------- |
| `show_header` | `bool` | `True` | Show the table header |
| `fixed_rows` | `int` | `0` | Number of fixed rows |
| `fixed_columns` | `int` | `0` | Number of fixed columns |
## Code blocks
```python
def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
\"\"\"Iterate and generate a tuple with a flag for last value.\"\"\"
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
for value in iter_values:
yield False, previous_value
previous_value = value
yield True, previous_value
```
"""
class MarkdownExampleApp(App):
def compose(self) -> ComposeResult:
markdown = Markdown(EXAMPLE_MARKDOWN)
markdown.code_indent_guides = False
yield markdown
if __name__ == "__main__":
app = MarkdownExampleApp()
app.run()
Reactive Attributes¶
This widget has no reactive attributes.
Messages¶
Bindings¶
This widget has no bindings.
Component Classes¶
The markdown widget provides the following component classes:
These component classes target standard inline markdown styles. Changing these will potentially break the standard markdown formatting.
Class | Description |
---|---|
code_inline |
Target text that is styled as inline code. |
em |
Target text that is emphasized inline. |
s |
Target text that is styled inline with strikethrough. |
strong |
Target text that is styled inline with strong. |
See Also¶
- MarkdownViewer code reference
Bases: Widget
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | None
|
String containing Markdown or None to leave blank for now. |
None
|
|
str | None
|
The name of the widget. |
None
|
|
str | None
|
The ID of the widget in the DOM. |
None
|
|
str | None
|
The CSS classes of the widget. |
None
|
|
Callable[[], MarkdownIt] | None
|
A factory function to return a configured MarkdownIt instance. If |
None
|
|
bool
|
Open links automatically. If you set this to |
True
|
COMPONENT_CLASSES
class-attribute
instance-attribute
¶
These component classes target standard inline markdown styles. Changing these will potentially break the standard markdown formatting.
Class | Description |
---|---|
code_inline |
Target text that is styled as inline code. |
em |
Target text that is emphasized inline. |
s |
Target text that is styled inline with strikethrough. |
strong |
Target text that is styled inline with strong. |
code_dark_theme
class-attribute
instance-attribute
¶
code_dark_theme = reactive('material')
The theme to use for code blocks when the App theme is dark.
code_indent_guides
class-attribute
instance-attribute
¶
code_indent_guides = reactive(True)
Should code fences display indent guides?
code_light_theme
class-attribute
instance-attribute
¶
code_light_theme = reactive('material-light')
The theme to use for code blocks when the App theme is light.
LinkClicked
¶
Bases: Message
A link in the document was clicked.
control
property
¶
The Markdown
widget containing the link clicked.
This is an alias for LinkClicked.markdown
and is used by the on
decorator.
TableOfContentsSelected
¶
Bases: Message
An item in the TOC was selected.
control
property
¶
The Markdown
widget where the selected item is.
This is an alias for TableOfContentsSelected.markdown
and is used by the on
decorator.
TableOfContentsUpdated
¶
Bases: Message
The table of contents was updated.
control
property
¶
The Markdown
widget associated with the table of contents.
This is an alias for TableOfContentsUpdated.markdown
and is used by the on
decorator.
markdown
instance-attribute
¶
The Markdown
widget associated with the table of contents.
goto_anchor
¶
goto_anchor(anchor)
Try and find the given anchor in the current document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The anchor to try and find. |
required |
Note
The anchor is found by looking at all of the headings in the document and finding the first one whose slug matches the anchor.
Note that the slugging method used is similar to that found on GitHub.
Returns:
Type | Description |
---|---|
bool
|
True when the anchor was found in the current document, False otherwise. |
load
async
¶
load(path)
Load a new Markdown document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Path
|
Path to the document. |
required |
Raises:
Type | Description |
---|---|
OSError
|
If there was some form of error loading the document. |
Note
The exceptions that can be raised by this method are all of
those that can be raised by calling Path.read_text
.
unhandled_token
¶
unhandled_token(token)
Process an unhandled token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Token
|
The MarkdownIt token to handle. |
required |
Returns:
Type | Description |
---|---|
MarkdownBlock | None
|
Either a widget to be added to the output, or |
update
¶
update(markdown)
Update the document with new Markdown.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
A string containing Markdown. |
required |
Returns:
Type | Description |
---|---|
AwaitComplete
|
An optionally awaitable object. Await this to ensure that all children have been mounted. |