Skip to content

Commit

Permalink
Fix bugs and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
xl0 committed Jul 28, 2024
1 parent f64e1c9 commit dfaabd8
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 105 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@
pip install friendlly
```

## ⚠️ At least for now, FriendLLy only works in Jupyter nbclassic!
## Front-end support

Due to tight intergration with the front-end, only `jupyter nbclassic`
(aka jupyter 6) is supported initially. You can install it alongside
`jupyter notebook` or `juter lab` without causing any conflicts
| Front-end | Markdown | Markdown cell | Code cell | Access current outputs | Access previous cells/outputs | Autorun cells |
|----|----|----|----|----|----|----|
| Jupyter nbclassic |||||||
| Jupyter VSCode || 🚫 |||| 🚫 |
| Jupyter Lab/notebook 7 || 🚫 ||| 🚫 | 🚫 |

⚠️ FriendLLy works best in Jupyter nbclassic, and is useable in VSCode.
Jupyter lab/notebook 7 support is somewhat limited for now.

You can install `jupyter nbclassic` alongside `jupyter notebook` or
`juter lab` without causing any conflicts:

pip install nbclassic

And rut it with
And run it with

jupyter nbclassic

Expand All @@ -37,7 +45,7 @@ import dotenv; dotenv.load_dotenv("../.env");
%load_ext friendlly

``` python
# %%fr
# %%fr
# Download an image of a cat, resize to 250 on long side, then very briefly describe it in the style of Jeremy Howard
```

Expand Down
6 changes: 3 additions & 3 deletions friendlly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
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
28 changes: 14 additions & 14 deletions friendlly/magic_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"""


# %% ../nbs/01_magic_cell.ipynb 7
# %% ../nbs/01_magic_cell.ipynb 9
import sys
import base64
import json
Expand Down Expand Up @@ -88,13 +88,13 @@
from IPython import get_ipython
from IPython.display import display, clear_output, Markdown, Javascript

# %% ../nbs/01_magic_cell.ipynb 8
# %% ../nbs/01_magic_cell.ipynb 10
# Patch the Jupyter nbclassic kernel.
if detect_environment() == "nbclassic":
nbclassic_patch_kernel()


# %% ../nbs/01_magic_cell.ipynb 9
# %% ../nbs/01_magic_cell.ipynb 11
def display_data_to_messages(dsp):
plain_text = dsp.get("text/plain")
text = list(filter(lambda x:x[1], [
Expand Down Expand Up @@ -173,7 +173,7 @@ def parse_above_cell(cell):
return is_friendlly, is_reply, code_text, output_blks
return False, False, None, []

# %% ../nbs/01_magic_cell.ipynb 10
# %% ../nbs/01_magic_cell.ipynb 12
def cells_to_messages(cells):
messages = []

Expand Down Expand Up @@ -208,7 +208,7 @@ def cells_to_messages(cells):

return [nict(m) for m in messages]

# %% ../nbs/01_magic_cell.ipynb 11
# %% ../nbs/01_magic_cell.ipynb 13
def merge_messages(msgs):

# first, merge by role
Expand All @@ -231,7 +231,7 @@ def merge_messages(msgs):
return role_messages


# %% ../nbs/01_magic_cell.ipynb 12
# %% ../nbs/01_magic_cell.ipynb 14
def make_cell_from_captured(source:str, res, stdout:str, stderr:str, displays, tracebacks):
ip = get_ipython()
cell = nict()
Expand All @@ -251,7 +251,7 @@ def make_cell_from_captured(source:str, res, stdout:str, stderr:str, displays, t

return cell

# %% ../nbs/01_magic_cell.ipynb 13
# %% ../nbs/01_magic_cell.ipynb 15
config = nict(
environment=detect_environment(),
autorun=detect_environment() == "nbclassic",
Expand Down Expand Up @@ -282,14 +282,14 @@ def handle_config(cell):
config.update(new_config)

if config.autorun and config.environment != "nbclassic":
warnings.warn_once("Autorun is only supported in nbclassic environment at the moment.")
warnings.warn("Autorun is only supported in nbclassic environment at the moment.")

if config.md_cells and config.environment != "nbclassic":
warnings.warn_once("Inserting Markdown cells is only supported in nbclassic environment at the moment.")
warnings.warn("Inserting Markdown cells is only supported in nbclassic environment at the moment.")
if config.api_key:
warnings.warn_once("FIY It's safer to set ANTHROPIC_API_KEY in the environment or even better .env file and use dotenv package to load it.")
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.")

# %% ../nbs/01_magic_cell.ipynb 14
# %% ../nbs/01_magic_cell.ipynb 16
class CellChat():
def __init__(self, config):
self.config = config
Expand Down Expand Up @@ -411,7 +411,7 @@ def combine_md_reply(body, thought, tail=""):
# display(Markdown(f"🚫 {repr(e)}"))
# get_ipython().showtraceback()

# %% ../nbs/01_magic_cell.ipynb 15
# %% ../nbs/01_magic_cell.ipynb 17
chat = None
def fr_cell(line=None, cell=None):
global chat
Expand All @@ -433,13 +433,13 @@ def fr_cell(line=None, cell=None):
cells_above = []
if config.environment == "nbclassic":
if "cells_above" not in header.content:
warnings.warn_once("Jupyter did not send any of the above cells")
warnings.warn("Jupyter did not send any of the above cells")
else:
cells_above = header.content.cells_above;
elif config.environment == "vscode":
idx, cells_above = vscode_get_cells(num_cells)
else:
warnings.warn_once("Only nbclassic and vscode environments can access previous cells")
warnings.warn("Only nbclassic and vscode environments can access previous cells")
prev_messages = cells_to_messages(cells_above)

if is_config:
Expand Down
194 changes: 117 additions & 77 deletions nbs/01_magic_cell.ipynb

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions nbs/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## ⚠️ At least for now, FriendLLy only works in Jupyter nbclassic!\n",
"\n",
"Due to tight intergration with the front-end, only `jupyter nbclassic` (aka jupyter 6) is supported initially.\n",
"You can install it alongside `jupyter notebook` or `juter lab` without causing any conflicts \n",
"\n",
"## Front-end support\n",
"| Front-end | Markdown | Markdown cell | Code cell | Access current outputs | Access previous cells/outputs | Autorun cells |\n",
"|----------|--------------------------|-----------------------------------|-----------------|-----------------------------------|------------------------|----------------|\n",
"| Jupyter nbclassic | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |\n",
"| Jupyter VSCode | ✅ | 🚫 | ✅ | ✅ | ✅ | 🚫 |\n",
"| Jupyter Lab/notebook 7 | ✅ | 🚫 | ✅ | ✅ | 🚫 | 🚫 |\n",
"\n",
"\n",
"⚠️ FriendLLy works best in Jupyter nbclassic, and is useable in VSCode. Jupyter lab/notebook 7 support is somewhat limited for now.\n",
"\n",
"You can install `jupyter nbclassic` alongside `jupyter notebook` or `juter lab` without causing any conflicts:\n",
"```\n",
"pip install nbclassic\n",
"```\n",
"And rut it with\n",
"And run it with\n",
"```\n",
"jupyter nbclassic\n",
"```\n"
Expand Down Expand Up @@ -105,7 +114,7 @@
"metadata": {},
"outputs": [],
"source": [
"# %%fr \n",
"# %%fr\n",
"# Download an image of a cat, resize to 250 on long side, then very briefly describe it in the style of Jeremy Howard"
]
},
Expand Down

0 comments on commit dfaabd8

Please sign in to comment.