# Display


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

This module enhances Jupyter’s display capabilities with:

- Rich rendering — Leverage `rich` library for beautiful object display
- Interactive JSON — Collapsible JSON viewer with configurable depth
- Dynamic CSS — Generate and update CSS stylesheets programmatically
- Global styles — Manage notebook-wide CSS that updates reactively

## Key Components

- [`pretty_repr`](https://civvic.github.io/pote/display.html#pretty_repr)
  — Render objects using `rich` formatting (HTML + text)
- [`rich_display`](https://civvic.github.io/pote/display.html#rich_display)
  — Display objects with rich formatting and updateable handles
- [`RenderJSON`](https://civvic.github.io/pote/display.html#renderjson)
  — Interactive collapsible JSON viewer
- [`cssmap`](https://civvic.github.io/pote/display.html#cssmap) —
  Convert Python dicts to CSS strings (including nested at-rules)
- [`GlobalCSS`](https://civvic.github.io/pote/display.html#globalcss) —
  Manage global CSS with reactive updates

## When to Use

- Pretty printing — Display complex data structures with syntax
  highlighting
- Data exploration — Inspect nested JSON/dicts with collapsible views
- Notebook styling — Generate CSS from Python for consistent theming
- Dynamic UIs — Update styles reactively based on state changes

<!-- # Prologue -->

# Rich display

Utilities for rendering objects with `rich` formatting in Jupyter. These
functions generate both HTML (for rich notebooks) and plain text (for
terminal/markdown) representations.

**Key features**: - Syntax highlighting for data structures -
Configurable output formats (HTML, text, or both) - Updateable display
handles for reactive UIs

``` python
from rich.json import JSON
from rich.jupyter import display as rich_display
```

``` python
def display_json(json):
    json_renderable = JSON.from_data(json)
    a = list(console.render(json_renderable))
    rich_display(a, console._render_buffer(a))
```

``` python
display_json({'a': 1, 'b': 2})
```

<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">{</span>
  <span style="color: #000080; text-decoration-color: #000080; font-weight: bold">"a"</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span>,
  <span style="color: #000080; text-decoration-color: #000080; font-weight: bold">"b"</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span>
<span style="font-weight: bold">}</span>
</pre>

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L32"
target="_blank" style="float:right; font-size:smaller">source</a>

### pretty_repr

``` python

def pretty_repr(
    o:VAR_POSITIONAL, html:bool=True, text:bool=True, kwargs:VAR_KEYWORD
)->dict[str, str] | str:

```

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L32"
target="_blank" style="float:right; font-size:smaller">source</a>

### pretty_repr

``` python

def pretty_repr(
    o:VAR_POSITIONAL, html:bool=True, text:bool=True, kwargs:VAR_KEYWORD
)->dict[str, str] | str:

```

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L32"
target="_blank" style="float:right; font-size:smaller">source</a>

### pretty_repr

``` python

def pretty_repr(
    o:VAR_POSITIONAL, html:bool=True, text:bool=True, kwargs:VAR_KEYWORD
)->dict[str, str] | str:

```

``` python
display(HTML(pretty_repr({'a': 1, 'b': [1,2,3]}, text=False)))
print(pretty_repr({'a': 1, 'b': [1,2,3]}, html=False))
cprint({'a': 1, 'b': [1,2,3]})
```

<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">{</span><span style="color: #008000; text-decoration-color: #008000">'a'</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span>, <span style="color: #008000; text-decoration-color: #008000">'b'</span>: <span style="font-weight: bold">[</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span><span style="font-weight: bold">]}</span>
</pre>

    {'a': 1, 'b': [1, 2, 3]}

<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">{</span><span style="color: #008000; text-decoration-color: #008000">'a'</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span>, <span style="color: #008000; text-decoration-color: #008000">'b'</span>: <span style="font-weight: bold">[</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span><span style="font-weight: bold">]}</span>
</pre>

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L41"
target="_blank" style="float:right; font-size:smaller">source</a>

### rich_display

``` python

def rich_display(
    o:VAR_POSITIONAL, dhdl:DisplayHandle | None=None
):

```

``` python
rich_display({'a': 1, 'b': 2}, [3, 4, 5])
```

<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">{</span><span style="color: #008000; text-decoration-color: #008000">'a'</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span>, <span style="color: #008000; text-decoration-color: #008000">'b'</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span><span style="font-weight: bold">}</span>
</pre>
&#10;<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">[</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">4</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">5</span><span style="font-weight: bold">]</span>
</pre>

``` python
dhdl = display(display_id=True)
rich_display({'a': 1, 'b': 2}, [3, 4, 5], dhdl=dhdl)
```

<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">{</span><span style="color: #008000; text-decoration-color: #008000">'a'</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span>, <span style="color: #008000; text-decoration-color: #008000">'b'</span>: <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span><span style="font-weight: bold">}</span>
</pre>
&#10;<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="font-weight: bold">[</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">4</span>, <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">5</span><span style="font-weight: bold">]</span>
</pre>

# Collapsible JSON

Interactive JSON viewer using
[renderjson](https://github.com/caldwell/renderjson). Displays JSON with
collapsible nodes, perfect for exploring deeply nested data.

**Features**: - Click to expand/collapse nodes - Configurable initial
expansion level - Scrollable container with max height - Automatic CDN
loading of renderjson library

**Use cases**: API responses, config files, deeply nested data
structures

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L49"
target="_blank" style="float:right; font-size:smaller">source</a>

### RenderJSON

``` python

def RenderJSON(
    json_data, max_height:int=200, init_level:int=0
):

```

*Initialize self. See help(type(self)) for accurate signature.*

``` python
json_data = {
    "name": "Petronila",
    "age": 28,
    "interests": ["reading", "cycling", "technology"],
    "education": {
        "bachelor": "Computer Science",
        "master": "Data Science",
        "phd": "Not enrolled"
    }
}

RenderJSON(json_data, init_level=1).display()
```

<div id="wrapper-0cd15e48-1cd2-4f12-9234-61ea008bf587" style="width: 100%; max-height: 200px; overflow-y: auto;">
    <div id="0cd15e48-1cd2-4f12-9234-61ea008bf587" style="width: 100%;"></div>
    <script>
        function renderMyJson() {
            renderjson.set_show_to_level(1);
            document.getElementById('0cd15e48-1cd2-4f12-9234-61ea008bf587').appendChild(renderjson({"name": "Petronila", "age": 28, "interests": ["reading", "cycling", "technology"], "education": {"bachelor": "Computer Science", "master": "Data Science", "phd": "Not enrolled"}}));
        };
        function loadRenderjson() {
            if (window.renderjson) return Promise.resolve();
            return new Promise((resolve, reject) => {
                const script = document.createElement('script');
                script.src = 'https://cdn.jsdelivr.net/npm/renderjson@latest/renderjson.js';
                script.onload = resolve;
                script.onerror = reject;
                document.head.appendChild(script);
            });
        };
        loadRenderjson().then(renderMyJson).catch(err => console.error('Failed to load renderjson:', err));
    </script>
</div>

# CSS

Utilities for generating and managing CSS in notebooks. Write styles in
Python dicts instead of CSS strings for better maintainability and
dynamic generation.

## cssmap

Convert Python dictionaries to CSS strings. Supports: - Nested
selectors - At-rules (`@media`, `@scope`, `@keyframes`, etc.) - Proper
indentation and formatting

**Why use this?** - Type-safe CSS generation - Easier to compose and
reuse styles - Programmatic style generation (loops, conditionals)

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L105"
target="_blank" style="float:right; font-size:smaller">source</a>

### cssmap

``` python

def cssmap(
    stylesheet, lvl:int=0
)->str:

```

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L105"
target="_blank" style="float:right; font-size:smaller">source</a>

### cssmap

``` python

def cssmap(
    stylesheet, lvl:int=0
)->str:

```

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L105"
target="_blank" style="float:right; font-size:smaller">source</a>

### cssmap

``` python

def cssmap(
    stylesheet, lvl:int=0
)->str:

```

``` python
expected = '''@scope (div.tableFixHead) {
  :scope {
    overflow-y: auto;
    max-width: max-content
  }

  thead th {
    position: sticky;
    top: 0px;
    background-color: gray
  }
}

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 20px
}'''
```

``` python
style = {
    '@scope (div.tableFixHead)': {
        ':scope': {
            'overflow-y': 'auto',
            'max-width': 'max-content',
        },
        'thead th': {
            'position': 'sticky',
            'top': '0px',
            'background-color': 'gray',
        }
    },
    'body': {
        'font-family': 'Arial, sans-serif',
        'margin': '0',
        'padding': '20px'
    }
}


css = cssmap(style)
test_eq(css, expected)
display(Markdown(f"```css\n{css}\n```"))
```

<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="background-color: #272822">                                                                                                                   
 </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">@</span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">scope</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822"> </span><span style="color: #ff4689; text-decoration-color: #ff4689; background-color: #272822">(div</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">.</span><span style="color: #a6e22e; text-decoration-color: #a6e22e; background-color: #272822">tableFixHead</span><span style="color: #ff4689; text-decoration-color: #ff4689; background-color: #272822">)</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822"> {</span><span style="background-color: #272822">                                                                                       </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">  :</span><span style="color: #a6e22e; text-decoration-color: #a6e22e; background-color: #272822">scope</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822"> {</span><span style="background-color: #272822">                                                                                                        </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">    </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">overflow-y</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">auto</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">;</span><span style="background-color: #272822">                                                                                             </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">    </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">max-width</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: max-content</span><span style="background-color: #272822">                                                                                        </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">  }</span><span style="background-color: #272822">                                                                                                               </span>
<span style="background-color: #272822">                                                                                                                   </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">  </span><span style="color: #ff4689; text-decoration-color: #ff4689; background-color: #272822">thead</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822"> </span><span style="color: #ff4689; text-decoration-color: #ff4689; background-color: #272822">th</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822"> {</span><span style="background-color: #272822">                                                                                                      </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">    </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">position</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">sticky</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">;</span><span style="background-color: #272822">                                                                                             </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">    </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">top</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: </span><span style="color: #ae81ff; text-decoration-color: #ae81ff; background-color: #272822">0</span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">px</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">;</span><span style="background-color: #272822">                                                                                                     </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">    </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">background-color</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">gray</span><span style="background-color: #272822">                                                                                        </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">  }</span><span style="background-color: #272822">                                                                                                               </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">}</span><span style="background-color: #272822">                                                                                                                 </span>
<span style="background-color: #272822">                                                                                                                   </span>
<span style="background-color: #272822"> </span><span style="color: #ff4689; text-decoration-color: #ff4689; background-color: #272822">body</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822"> {</span><span style="background-color: #272822">                                                                                                            </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">  </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">font-family</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: Arial, </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">sans-serif</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">;</span><span style="background-color: #272822">                                                                                 </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">  </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">margin</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: </span><span style="color: #ae81ff; text-decoration-color: #ae81ff; background-color: #272822">0</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">;</span><span style="background-color: #272822">                                                                                                      </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">  </span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">padding</span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">: </span><span style="color: #ae81ff; text-decoration-color: #ae81ff; background-color: #272822">20</span><span style="color: #66d9ef; text-decoration-color: #66d9ef; background-color: #272822">px</span><span style="background-color: #272822">                                                                                                   </span>
<span style="background-color: #272822"> </span><span style="color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822">}</span><span style="background-color: #272822">                                                                                                                 </span>
<span style="background-color: #272822">                                                                                                                   
</span></pre>

## GlobalCSS

Manage notebook-wide CSS with reactive updates. Create a single
[`GlobalCSS`](https://civvic.github.io/pote/display.html#globalcss)
instance, display it once, then add/update styles dynamically throughout
your notebook.

**Pattern**:

``` python
# Cell 1: Initialize and display
GCSS = GlobalCSS()
display(GCSS)

# Cell 2: Add styles dynamically
GCSS.add('my-component', '.my-component { color: blue; }')

# Cell 3: Update existing styles
GCSS.update('my-component', '.my-component { color: red; }')
```

**Benefits**: - Single source of truth for notebook styles - No
duplicate `<style>` tags - Reactive updates without re-executing cells -
Named styles for easy management

------------------------------------------------------------------------

<a href="https://github.com/civvic/pote/blob/main/pote/display.py#L124"
target="_blank" style="float:right; font-size:smaller">source</a>

### GlobalCSS

``` python

def GlobalCSS(
    css:dict[str, str] | None=None
):

```

*Initialize self. See help(type(self)) for accurate signature.*

``` python
GCSS = GlobalCSS()
display(GCSS)
```

<style id="test">.__test__gcss__ { color: green; }</style>

``` python
<div class="__test__gcss__">Hello</div>
```

<div class="__test__gcss__">Hello</div>

``` python
GCSS.add('test', '.__test__gcss__ { color: red; }')
```

``` python
GCSS.update('test', '.__test__gcss__ { color: green; }')
```

``` python
test_stdout(lambda: display(GCSS), 'GlobalCSSs should be displayed only once, skipping.')
```

------------------------------------------------------------------------

<!-- # Colophon -->
