Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 603788112
  • Loading branch information
blois authored and colaboratory-team committed Feb 2, 2024
1 parent 2c57fc6 commit b1eaf58
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions google/colab/_reprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io
import json
import types
import uuid
import warnings
from google.colab import _inspector
# pytype: disable=import-error
Expand Down Expand Up @@ -359,8 +360,42 @@ def _image_repr(ndarray: np.ndarray):
encoded = base64.b64encode(buffered.getvalue()).decode('utf-8')
uri = 'data:image/png;base64,' + encoded

result = f'<pre>ndarray shape: {ndarray.shape} dtype: {ndarray.dtype}</pre>'
result += f'<img src="{uri}" />'
result = """<style>
.ndarray_repr .ndarray_raw_data {
display: none;
}
.ndarray_repr.show_array .ndarray_raw_data {
display: block;
}
.ndarray_repr.show_array .ndarray_image_preview {
display: none;
}
</style>
"""
html_id = f'id-{uuid.uuid4()}'
result += f'<div id="{html_id}" class="ndarray_repr">'

result += f'<pre>ndarray {ndarray.shape} <a href="#">show data</a></pre>'
result += f'<img src="{uri}" class="ndarray_image_preview" />'

result += (
f'<pre class="ndarray_raw_data">{html.escape(ndarray.__repr__())}</pre>'
)
result += '</div>'

result += f"""<script>
(() => {{
const titles = ['show data', 'hide data'];
let index = 0
document.querySelector('#{html_id} a').onclick = (e) => {{
document.querySelector('#{html_id}').classList.toggle('show_array');
index = (++index) % 2;
document.querySelector('#{html_id} a').textContent = titles[index];
e.preventDefault();
e.stopPropagation();
}}
}})();
</script>"""
return result
except Exception: # pylint: disable=broad-except
return None
Expand Down

0 comments on commit b1eaf58

Please sign in to comment.