MCPcopy Create free account
hub / github.com/datapane/datapane / format

Method format

python-client/src/datapane/blocks/text.py:62–100  ·  view source on GitHub ↗

Format the markdown text template, using the supplied context to insert blocks into `{{}}` markers in the template. `{}` markers can be empty, hence positional, or have a name, e.g. `{{plot}}`, which is used to lookup the value from the keyword context. Args: *

(self, *args: BlockOrPrimitive, **kwargs: BlockOrPrimitive)

Source from the content-addressed store, hash-verified

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]))
90 else:
91 blocks.append(wrap_block(deque_args.popleft()))
92 except (IndexError, KeyError):
93 raise DPClientError(f"Unknown/missing object '{x}' referenced in Markdown format")
94
95 else:
96 x = x.strip()
97 if x:
98 blocks.append(Text(x))
99
100 return Group(blocks=blocks)
101
102
103class Code(EmbeddedTextBlock):

Callers 7

build_md_viewFunction · 0.95
formatNumberFunction · 0.80
formatNumberFunction · 0.80
constructorMethod · 0.80
demoFunction · 0.80
display_msgFunction · 0.80
test_markdown_formatFunction · 0.80

Calls 5

DPClientErrorClass · 0.90
wrap_blockFunction · 0.85
TextClass · 0.85
GroupClass · 0.70
appendMethod · 0.45

Tested by 1

test_markdown_formatFunction · 0.64