Skip to content

Commit

Permalink
Cleanup and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xl0 committed Jul 29, 2024
1 parent c845b0c commit b8406a8
Show file tree
Hide file tree
Showing 27 changed files with 2,857 additions and 2,611 deletions.
10 changes: 5 additions & 5 deletions friendlly/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
__version__ = "0.1.1"
__version__ = "0.2.0"

from .magic_cell import fr_cell
from .magic_line import fr_line
from .utils.notebook import nbclassic_patch_kernel, detect_environment
from .notebook import nbclassic_patch_kernel, detect_environment
from IPython.display import clear_output

def load_ipython_extension(ipython):
# if detect_environment() == "nbclassic":
# nbclassic_patch_kernel()
# clear_output()
if detect_environment() == "nbclassic":
nbclassic_patch_kernel()
clear_output()

ipython.register_magic_function(fr_cell, "cell", magic_name="fr")
ipython.register_magic_function(fr_line, "line", magic_name="fr")
Expand Down
182 changes: 75 additions & 107 deletions friendlly/_modidx.py

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions friendlly/capture_display.py
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

6 changes: 3 additions & 3 deletions friendlly/utils/capture_io.py → friendlly/capture_io.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/03_utils.capture_io.ipynb.
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_capture_io.ipynb.

# %% auto 0
__all__ = ['TeeIO']

# %% ../../nbs/03_utils.capture_io.ipynb 2
# %% ../nbs/03_capture_io.ipynb 2
from io import StringIO

# %% ../../nbs/03_utils.capture_io.ipynb 3
# %% ../nbs/03_capture_io.ipynb 3
class TeeIO:
"""
OutStream that and also passes it to the original stream.
Expand Down
18 changes: 5 additions & 13 deletions friendlly/utils/capture_trace.py → friendlly/capture_trace.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/03_utils.capture_trace.ipynb.
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_capture_trace.ipynb.

# %% auto 0
__all__ = ['strip_junk', 'SaveTraceback']
__all__ = ['SaveTraceback']

# %% ../../nbs/03_utils.capture_trace.ipynb 2
# %% ../nbs/03_capture_trace.ipynb 2
import re
from IPython import get_ipython
from .misc import nict
from .utils import nict

# %% ../../nbs/03_utils.capture_trace.ipynb 3
def strip_junk(text):
# This line just easts up tokens. We don't need it.
text = text.replace("---------------------------------------------------------------------------", "")
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)


# %% ../../nbs/03_utils.capture_trace.ipynb 5
# %% ../nbs/03_capture_trace.ipynb 3
class SaveTraceback:
def __init__(self, saved):
self._saved_showtraceback = saved
Expand Down
36 changes: 36 additions & 0 deletions friendlly/config.py
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.")
Loading

0 comments on commit b8406a8

Please sign in to comment.