Skip to content

Commit 8902d29

Browse files
authored
Include a plain text repr for when notebook is untrusted. (#1577)
* Store a plain repr for when a notebook is untrusted (github) * Run quickstart notebook * Run tutorial notebooks that have reprs
1 parent 4748c0b commit 8902d29

File tree

9 files changed

+4176
-2883
lines changed

9 files changed

+4176
-2883
lines changed

docs/quickstart.ipynb

Lines changed: 621 additions & 378 deletions
Large diffs are not rendered by default.

docs/tutorials/how-to-create-stac-catalogs.ipynb

Lines changed: 1610 additions & 878 deletions
Large diffs are not rendered by default.

docs/tutorials/pystac-introduction.ipynb

Lines changed: 1918 additions & 1620 deletions
Large diffs are not rendered by default.

pystac/asset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def _repr_html_(self) -> str:
185185
jinja_env = get_jinja_env()
186186
if jinja_env:
187187
template = jinja_env.get_template("JSON.jinja2")
188-
return str(template.render(dict=self.to_dict()))
188+
return str(template.render(dict=self.to_dict(), plain=escape(repr(self))))
189189
else:
190190
return escape(repr(self))
191191

pystac/html/JSON.jinja2

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@
2929
color: rgb(0, 128, 0);
3030
font-weight: 700;
3131
}
32+
.pystac-wrap {
33+
display: block !important;
34+
}
35+
.pystac-text-repr-fallback {
36+
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
37+
display: none;
38+
}
3239
</style>
33-
<div class="jp-RenderedJSON jp-mod-trusted jp-OutputArea-output">
40+
<div>
41+
<div class="pystac-text-repr-fallback">
42+
{{ plain }}
43+
</div>
44+
<div class="jp-RenderedJSON jp-mod-trusted jp-OutputArea-output pystac-wrap" style='display:none'>
3445
<div class="container" style="line-height: normal;">
3546
<ul style="padding: 0px; margin: 0px; list-style: none; display: block;">
3647
{% for key, value in dict.items() %}
@@ -42,4 +53,5 @@
4253
{% endfor %}
4354
</ul>
4455
</div>
45-
</div>
56+
</div>
57+
</div>

pystac/item_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _repr_html_(self) -> str:
150150
jinja_env = get_jinja_env()
151151
if jinja_env:
152152
template = jinja_env.get_template("JSON.jinja2")
153-
return str(template.render(dict=self.to_dict()))
153+
return str(template.render(dict=self.to_dict(), plain=escape(repr(self))))
154154
else:
155155
return escape(repr(self))
156156

pystac/link.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ def _repr_html_(self) -> str:
275275
jinja_env = get_jinja_env()
276276
if jinja_env:
277277
template = jinja_env.get_template("JSON.jinja2")
278-
return str(template.render(dict=self.to_dict()))
278+
return str(
279+
template.render(
280+
dict=self.to_dict(transform_href=False), plain=escape(repr(self))
281+
)
282+
)
279283
else:
280284
return escape(repr(self))
281285

pystac/provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _repr_html_(self) -> str:
7676
jinja_env = get_jinja_env()
7777
if jinja_env:
7878
template = jinja_env.get_template("JSON.jinja2")
79-
return str(template.render(dict=self.to_dict()))
79+
return str(template.render(dict=self.to_dict(), plain=escape(repr(self))))
8080
else:
8181
return escape(repr(self))
8282

pystac/stac_object.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,11 @@ def _repr_html_(self) -> str:
587587
jinja_env = get_jinja_env()
588588
if jinja_env:
589589
template = jinja_env.get_template("JSON.jinja2")
590-
return str(template.render(dict=self.to_dict(transform_hrefs=False)))
590+
return str(
591+
template.render(
592+
dict=self.to_dict(transform_hrefs=False), plain=escape(repr(self))
593+
)
594+
)
591595
else:
592596
return escape(repr(self))
593597

0 commit comments

Comments
 (0)