a
    ijB                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlZddl	Z	ddl	m
Z
mZmZ ddlZddlZe	jreeB eB eB dB Zeeedf B ed B ZedZejddd	d
dddddZdddddddZdddddZdS )zColab utils.    )annotationsN)IteratorIterableTypeVarJson_T F)expandedstrboolzIterator[None])namer	   returnc                c  s   ddl }| }|j|gd}|d|  |r6d|_nd|_tj| |J z
dV  W n* ty } z|} W Y d}~nd}~0 0 d}W d   n1 s0    Y  |dur|dS )zCapture all outputs and display it in a collapsible block.

  Args:
    name: Name of the collapsible section.
    expanded: If `True`, the section is expanded by default.

  Yields:
    None
  r   N)children)
ipywidgetsOutputZ	Accordion	set_titleZselected_indexIPythondisplay	Exception)r   r	   r   outZ	accordioneexc r   V/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/ecolab/colab_utils.pycollapse%   s"    
"r   None)valuer	   r   c                 C  s   t  j}d}tt| }|r.d| dnd}d| d| d| d| d	| d
| d| d| d| d}tjtj	| dS )a  Display the Json `dict` / `list` interactivelly (with collapsible elems).

  Examples:

  ```python
  ecolab.json({'a': [1, 2, 3], 'b': {'x': True, 'y': False}})
  ```

  The dict keys and list indices can be filtered from the display field using
  regex (e.g. `a.[0-9]` in the above example).

  Args:
    value: Json `dict` or `list` to inspect.
    expanded: Whether the elements start as expanded or as collapsed.
  a/  
  json-viewer {
    padding:1px 1.5em 1px 1.5em;
    --background-color: #f7f7f7;
    --property-color: #087db2;
    --string-color: #a31515;
    --number-color: #008000;
    --boolean-color: #0000ff;
    --null-color: #af00db;
    --preview-color: #888888;
  }
  json-viewer::part(key) {
    margin-right: 0.5em;
  }
  html[theme=dark] json-viewer {
    --background-color: #2c2c2c;
    --property-color: #6fb3d2;
    --string-color: #ce9178;
    --number-color: #b5cea8;
    --boolean-color: #569cd6;
    --null-color: #c586c0;
    --preview-color: #888888;
  }
  .ecolab-json button {
      background-color: var(--colab-highlighted-surface-color);
      color: var(--colab-primary-text-color);
      border-width: 0;
  }
  .ecolab-json input {
      border-color: var(--colab-highlighted-surface-color);
  }
  Zviewerz.expandAll();
r   z
  <script src="https://unpkg.com/@alenaksu/json-viewer@2.0.0/dist/json-viewer.bundle.js"></script>
  <script>
    const viewerz  = document.querySelector('#jsonz');
    z
  </script>
  <style>
  zC
  </style>
  <div class="ecolab-json">
    <button onclick="viewerz>.expandAll();">Expand All</button>
    <button onclick="viewerz\.collapseAll();">Collapse All</button>
    <input placeholder="Filter Regex" onkeyup="viewerzE.filter(RegExp(this.value, 'i'));"></input>
    <json-viewer id="jsonz">z</json-viewer>
  </div>
  N)
uuiduuid1hexhtmlescapejson_stddumpsr   r   HTML)r   r	   Zid_Zcss_contentZhtml_contentZexpand_liner   r   r   jsonH   s0    
!
r%   zIterable[_T] | Iterator[_T]zIterator[_T])innerr   c              
   #  sx   t    fdd}ttj|}z@| D ](}|V    r( W ttj| dS q(W ttj| nttj| 0 dS )a  Catch KeyboardInterrupts to end the loop without raising an Exception.

  While this iterator is running, the first SIGINT (e.g. from interrupting
  the colab runtime, or pressing Ctrl+C ) will not raise an exception but
  instead end the loop after the current iteration.
  A second SIGINT will raise a KeyboardInterrupt Exception as usual.

  Examples:

  ```python
  for i in interruptible(range(10)):
    print(i)
    time.sleep(3)
  ```

  Args:
    inner: arbitrary iterable or iterator

  Yields:
    elements from inner
  c                   s      rt   d S )N)is_setKeyboardInterruptset)Zunused_signumZunused_frameinterruptedr   r   handler   s    zinterruptible.<locals>.handlerN)	threadingEventsignalSIGINTr'   )r&   r,   Zprevious_handlerir   r*   r   interruptible   s    r2   )r   )F)__doc__
__future__r   
contextlibr    r%   r"   r/   r-   typingr   r   r   r   IPython.displayr   TYPE_CHECKINGr
   floatintr   Z	JsonValuedictlistr   r   contextmanagerr   r2   r   r   r   r   <module>   s$   "P