-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
2,857 additions
and
2,611 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_capture_display.ipynb. | ||
|
||
# %% auto 0 | ||
__all__ = ['PassthroughCapturer'] | ||
|
||
# %% ../nbs/02_capture_display.ipynb 2 | ||
from IPython.core.displaypub import DisplayPublisher | ||
from IPython.core.display_functions import _new_id | ||
from IPython.display import display, clear_output, Javascript | ||
from IPython import get_ipython | ||
from typing import List | ||
from PIL import Image | ||
from collections import defaultdict | ||
from io import BytesIO | ||
import base64 | ||
|
||
from .utils import nict | ||
|
||
# %% ../nbs/02_capture_display.ipynb 3 | ||
class PassthroughCapturer(DisplayPublisher): | ||
"""A DisplayPublisher that stores AND displays!""" | ||
|
||
raw_outputs: List | ||
|
||
def __init__(self): | ||
self._publisher = None | ||
self.raw_outputs = [] | ||
|
||
def publish( | ||
self, data, metadata=None, source=None, *, transient=None, update=False | ||
): | ||
self.raw_outputs.append(nict(data=data, metadata=metadata, transient=transient, update=update)) | ||
self._publisher.publish(data, metadata=metadata, transient=transient, update=update) | ||
|
||
def clear_output(self, wait=False): | ||
self.raw_outputs = [] | ||
self._publisher.clear_output(wait) | ||
|
||
def start(self): | ||
ip = get_ipython() | ||
self._publisher = ip.display_pub | ||
ip.display_pub = self | ||
|
||
# print("starting passthrough: ", id(self._publisher), id(self)) | ||
|
||
# Note: stop must be called from the cell that started the | ||
# passthrough, or from the associated post-cell callback | ||
def stop(self): | ||
ip = get_ipython() | ||
assert self._publisher | ||
ip.display_pub = self._publisher | ||
|
||
def get_outputs(self, with_js=False): | ||
coalesced_outputs = [] | ||
display_id_map = defaultdict(list) | ||
|
||
outputs = self.raw_outputs | ||
if not with_js: | ||
outputs = [o for o in outputs if 'application/javascript' not in o['data']] | ||
|
||
# dicts are ordered in Python 3.7+ | ||
for output in outputs: | ||
display_id = output['transient']['display_id'] if output['transient'] else _new_id() | ||
if not output['update']: | ||
display_id_map[display_id] += [output["data"]] | ||
else: | ||
display_id_map[display_id][-1] = output["data"] | ||
|
||
for display_id, output_list in display_id_map.items(): | ||
coalesced_outputs += output_list | ||
|
||
return coalesced_outputs | ||
|
||
|
||
# %% ../nbs/02_capture_display.ipynb 6 | ||
#| export | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 5 additions & 13 deletions
18
friendlly/utils/capture_trace.py → friendlly/capture_trace.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_config.ipynb. | ||
|
||
# %% auto 0 | ||
__all__ = ['strip_comments', 'handle_config'] | ||
|
||
# %% ../nbs/03_config.ipynb 2 | ||
def strip_comments(source: str): | ||
"""Strips Python # comments""" | ||
lines = source.splitlines() | ||
return "\n".join([ l.split("#")[0] for l in lines ]).strip() | ||
|
||
def handle_config(cell, global_config): | ||
|
||
cell = strip_comments(cell) | ||
# If the cell was empty, show the config | ||
if not cell: | ||
def value_to_str(v): | ||
if isinstance(v, str) and "\n" in v: | ||
return f'"""{v}"""' | ||
return str(v) | ||
|
||
cell = "\n".join([f"{k}={value_to_str(v)}" for k, v in global_config.items()]) | ||
update_code_self("%%fr config\n" + cell) | ||
else: | ||
new_config = {} | ||
# Code, globals, locals. We want the locals | ||
exec(cell, {}, new_config) | ||
global_config.update(new_config) | ||
|
||
if global_config.autorun and global_config.environment != "nbclassic": | ||
warnings.warn("Autorun is only supported in nbclassic environment at the moment.") | ||
|
||
if global_config.md_cells and global_config.environment != "nbclassic": | ||
warnings.warn("Inserting Markdown cells is only supported in nbclassic environment at the moment.") | ||
if global_config.api_key: | ||
warnings.warn("FIY It's safer to set ANTHROPIC_API_KEY in the environment or even better .env file and use dotenv package to load it.") |
Oops, something went wrong.