You can add short or long-form Markdown content to your app with the `Text` block. !!! info Markdown is a lightweight markup language that allows you to include formatted text in your app, and can be accessed through `dp.Text`, or by passing in a string directly.  Che
| 30 | |
| 31 | |
| 32 | class Text(EmbeddedTextBlock): |
| 33 | """ |
| 34 | You can add short or long-form Markdown content to your app with the `Text` block. |
| 35 | |
| 36 | !!! info |
| 37 | Markdown is a lightweight markup language that allows you to include formatted text in your app, and can be accessed through `dp.Text`, or by passing in a string directly.  |
| 38 | |
| 39 | Check [here](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) for more information on how to format your text with markdown. |
| 40 | """ |
| 41 | |
| 42 | _tag = "Text" |
| 43 | |
| 44 | def __init__(self, text: str = None, file: NPath = None, name: BlockId = None, label: str = None): |
| 45 | """ |
| 46 | Args: |
| 47 | text: The markdown formatted text, use triple-quotes, (`\"\"\"# My Title\"\"\"`) to create multi-line markdown text |
| 48 | file: Path to a file containing markdown text |
| 49 | name: A unique name for the block to reference when adding text or embedding (optional) |
| 50 | label: A label used when displaying the block (optional) |
| 51 | |
| 52 | !!! note |
| 53 | File encodings are auto-detected, if this fails please read the file manually with an explicit encoding and use the text parameter on dp.Attachment |
| 54 | """ |
| 55 | if text: |
| 56 | text = textwrap.dedent(text).strip() |
| 57 | |
| 58 | assert text or file |
| 59 | content = text or utf_read_text(Path(file).expanduser()) |
| 60 | super().__init__(content=content, name=name, label=label) |
| 61 | |
| 62 | def format(self, *args: BlockOrPrimitive, **kwargs: BlockOrPrimitive) -> Group: |
| 63 | """ |
| 64 | Format the markdown text template, using the supplied context to insert blocks into `{{}}` markers in the template. |
| 65 | |
| 66 | `{}` markers can be empty, hence positional, or have a name, e.g. `{{plot}}`, which is used to lookup the value from the keyword context. |
| 67 | |
| 68 | Args: |
| 69 | *args: positional template context arguments |
| 70 | **kwargs: keyword template context arguments |
| 71 | |
| 72 | !!! tip |
| 73 | Either Python objects, e.g. dataframes, and plots, or Datapane blocks as context |
| 74 | |
| 75 | Returns: |
| 76 | A datapane Group object containing the list of text and embedded objects |
| 77 | """ |
| 78 | |
| 79 | splits = re.split(r"\{\{(\w*)\}\}", self.content) |
| 80 | deque_args = deque(args) |
| 81 | blocks = [] |
| 82 | |
| 83 | for (i, x) in enumerate(splits): |
| 84 | is_block = bool(i % 2) |
| 85 | |
| 86 | if is_block: |
| 87 | try: |
| 88 | if x: |
| 89 | blocks.append(wrap_block(kwargs[x])) |
no outgoing calls
no test coverage detected