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

Class Blocks

python-client/src/datapane/view/view_blocks.py:20–89  ·  view source on GitHub ↗

Container that holds a collection of blocks

Source from the content-addressed store, hash-verified

18
19
20class Blocks(ContainerBlock):
21 """Container that holds a collection of blocks"""
22
23 # This is essentially an easy-to-use wrapper around a list of blocks
24 # that is composable.
25 # TODO - move to datapane.blocks ?
26
27 _tag = "Blocks"
28
29 def __init__(self, *arg_blocks: BlockOrPrimitive, blocks: t.List[BlockOrPrimitive] = None, **kwargs):
30 # if passed a single View into a View object, pull out the contained blocks and use instead
31 if len(arg_blocks) == 1 and isinstance(arg_blocks[0], Blocks):
32 arg_blocks = tuple(arg_blocks[0].blocks)
33
34 super().__init__(*arg_blocks, blocks=blocks, **kwargs)
35
36 def __or__(self, other: Blocks) -> Blocks:
37 x = Group(blocks=self.blocks) if len(self.blocks) > 1 else self.blocks[0]
38 y = Group(blocks=other.blocks) if len(other.blocks) > 1 else other.blocks[0]
39 z = Group(x, y, columns=2)
40 return Blocks(z)
41
42 @classmethod
43 def from_notebook(
44 cls, opt_out: bool = True, show_code: bool = False, show_markdown: bool = True, template: str = "auto"
45 ) -> Self:
46 from datapane.ipython import templates as ip_t
47 from datapane.ipython.utils import cells_to_blocks
48
49 blocks = cells_to_blocks(opt_out=opt_out, show_code=show_code, show_markdown=show_markdown)
50 app_template_cls = ip_t._registry.get(template) or ip_t.guess_template(blocks)
51 app_template = app_template_cls(blocks)
52 app_template.transform()
53 app_template.validate()
54 return cls(blocks=app_template.blocks)
55
56 def get_dom(self) -> ElementT:
57 """Return the Document structure for the View"""
58 # internal debugging method
59 from datapane.processors.file_store import DummyFileEntry, FileStore
60
61 from .xml_visitor import XMLBuilder
62
63 builder = XMLBuilder(FileStore(DummyFileEntry))
64 self.accept(builder)
65 return builder.get_root()
66
67 def get_dom_str(self) -> str:
68 dom = self.get_dom()
69 return etree.tounicode(dom, pretty_print=True)
70
71 def pprint(self) -> None:
72 from .visitors import PrettyPrinter
73
74 self.accept(PrettyPrinter())
75
76 @classmethod
77 def wrap_blocks(cls, x: t.Union[Self, t.List[BlockOrPrimitive], BlockOrPrimitive]) -> Self:

Callers 4

build_md_viewFunction · 0.90
demoFunction · 0.90
_ipython_display_Method · 0.90
__or__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected