55from .inline_utils import _is_url , _process_markdown
66
77
8+
89__all__ = [
910 "Block" ,
1011 "BlockList" ,
@@ -48,6 +49,22 @@ def _to_mjml(self) -> MJMLTag:
4849 """
4950 return self ._mjml_tag
5051
52+ def _repr_html_ (self ) -> str :
53+ """
54+ Return HTML representation for rich display in Jupyter notebooks.
55+
56+ Examples
57+ --------
58+ ```{python}
59+ from nbmail.compose import block_text
60+
61+ block = block_text("Hello world!")
62+ block
63+ ```
64+ """
65+ block_list = BlockList (self )
66+ return block_list ._repr_html_ ()
67+
5168
5269class BlockList :
5370 """
@@ -62,7 +79,7 @@ class BlockList:
6279 --------
6380 Users typically create BlockList via the `create_blocks()` function:
6481
65- ```python
82+ ```{ python}
6683 from nbmail.compose import create_blocks, block_text, block_title
6784
6885 content = create_blocks(
@@ -80,6 +97,36 @@ def __init__(self, *args: Union["Block", str]):
8097 One or more `Block` objects or strings.
8198 """
8299 self .sections = list (args )
100+
101+ def _repr_html_ (self ) -> str :
102+ """
103+ Return HTML representation for rich display in Jupyter notebooks.
104+
105+ This method wraps the BlockList in a compose_email() call and delegates
106+ to the Email's _repr_html_() method for rendering, enabling interactive
107+ preview of blocks directly in notebooks.
108+
109+ Returns
110+ -------
111+ str
112+ HTML content with inline attachments embedded as base64 data URIs.
113+
114+ Examples
115+ --------
116+ ```{python}
117+ from nbmail.compose import create_blocks, block_title, block_text
118+
119+ content = create_blocks(
120+ block_title("My Email"),
121+ block_text("Hello world!")
122+ )
123+ content
124+ ```
125+ """
126+ from .compose import compose_email
127+
128+ email = compose_email (body = self )
129+ return email ._repr_html_ ()
83130
84131 def _to_mjml_list (self ) -> list [MJMLTag ]:
85132 """
@@ -134,12 +181,14 @@ def block_text(
134181
135182 Examples
136183 --------
137- ```python
184+ ```{ python}
138185 from nbmail.compose import block_text
139186
140187 # Simple text
141188 block = block_text("Hello world")
142189
190+ print(1+1)
191+
143192 # Markdown text
144193 block = block_text("This is **bold** and this is *italic*")
145194
@@ -179,14 +228,10 @@ def block_title(
179228
180229 Examples
181230 --------
182- ```python
231+ ```{ python}
183232 from nbmail.compose import block_title
184233
185- # Simple title
186- title = block_title("My Newsletter")
187-
188- # Centered title (default)
189- title = block_title("Welcome!", align="center")
234+ block_title("My Newsletter")
190235 ```
191236 """
192237
@@ -226,10 +271,10 @@ def block_spacer(height: str = "20px") -> Block:
226271
227272 Examples
228273 --------
229- ```python
274+ ```{ python}
230275 from nbmail.compose import block_spacer, create_blocks, block_text
231276
232- email_body = create_blocks(
277+ create_blocks(
233278 block_text("First section"),
234279 block_spacer("30px"),
235280 block_text("Second section"),
@@ -292,14 +337,14 @@ def block_image(
292337
293338 Examples
294339 --------
295- ```python
296- from nbmail.compose import block_image, create_blocks, block_text
340+ ```{ python}
341+ from nbmail.compose import block_image, create_blocks, block_text, compose_email, md
297342
298- email = compose_email(
343+ compose_email(
344+ title="Product Feature",
299345 body=create_blocks(
300- block_text("Here's an image:"),
301- block_image("path/to/image.png", alt="Example", width=600),
302- block_text("And some text after it.")
346+ block_text(md("Check this out:")),
347+ block_image("https://fastly.picsum.photos/id/630/300/200.jpg?hmac=dSM5_yM5Z9Pb3CX6OviVW3dEbyHmkD04otrIKU2LQ50", alt="Product image", width="500px")
303348 )
304349 )
305350 ```
@@ -409,16 +454,17 @@ def block_plot(
409454
410455 Examples
411456 --------
412- ```python
413- from nbmail.compose import block_plot, create_blocks, block_text
414- from plotnine import ggplot, aes, geom_point, mtcars
457+ ```{python}
458+ from nbmail.compose import block_plot, create_blocks, block_text, compose_email
459+ from plotnine import ggplot, aes, geom_point
460+ from great_tables.data import gtcars
415461
416462 plot = (
417- ggplot(mtcars , aes("disp ", "hp"))
463+ ggplot(gtcars , aes("trq ", "hp"))
418464 + geom_point()
419465 )
420466
421- email = compose_email(
467+ compose_email(
422468 body=create_blocks(
423469 block_text("Here's my plot:"),
424470 block_plot(plot, alt="Scatter plot"),
0 commit comments