Skip to content

Commit e25fc10

Browse files
fix: Enhance Markdown component to support indented multi-line strings (#2634)
Markdown card component failed to render multiline strings with indentation: ```python # Didn't render - leading whitespace broke Markdown parsing Markdown(f""" # Header - Item 1 """) ``` Added automatic text dedenting using `textwrap.dedent()` in the Markdown component. ```python # Now works automatically! Markdown(f""" # Header - Item 1 """) ```
1 parent 0092d10 commit e25fc10

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

metaflow/plugins/cards/card_modules/components.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .json_viewer import JSONViewer as _JSONViewer, YAMLViewer as _YAMLViewer
1616
import uuid
1717
import inspect
18+
import textwrap
1819

1920

2021
def _warning_with_component(component, msg):
@@ -656,19 +657,38 @@ class Markdown(UserComponent):
656657
)
657658
```
658659
660+
Multi-line strings with indentation are automatically dedented:
661+
```
662+
current.card.append(
663+
Markdown(f'''
664+
# Header
665+
- Item 1
666+
- Item 2
667+
''')
668+
)
669+
```
670+
659671
Parameters
660672
----------
661673
text : str
662-
Text formatted in Markdown.
674+
Text formatted in Markdown. Leading whitespace common to all lines
675+
is automatically removed to support indented multi-line strings.
663676
"""
664677

665678
REALTIME_UPDATABLE = True
666679

680+
@staticmethod
681+
def _dedent_text(text):
682+
"""Remove common leading whitespace from all lines."""
683+
if text is None:
684+
return None
685+
return textwrap.dedent(text)
686+
667687
def update(self, text=None):
668-
self._text = text
688+
self._text = self._dedent_text(text)
669689

670690
def __init__(self, text=None):
671-
self._text = text
691+
self._text = self._dedent_text(text)
672692

673693
@with_default_component_id
674694
@render_safely

0 commit comments

Comments
 (0)