Skip to content

Commit

Permalink
⛓️ fix ipython streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Jul 19, 2024
1 parent 7f0413f commit f99eb64
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 110 deletions.
16 changes: 10 additions & 6 deletions src/codeboxapi/codebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def exec(
timeout: float | None = None,
cwd: str | None = None,
) -> ExecResult:
"""Execute python code inside the CodeBox instance"""
"""Execute code inside the CodeBox instance"""
return flatten_exec_result(self.stream_exec(code, kernel, timeout, cwd))

def stream_exec(
Expand All @@ -102,7 +102,7 @@ def stream_exec(
timeout: float | None = None,
cwd: str | None = None,
) -> Generator[ExecChunk, None, None]:
"""Stream Chunks of Execute python code inside the CodeBox instance"""
"""Executes the code and streams the result."""
raise NotImplementedError("Abstract method, please use a subclass.")

def upload(
Expand Down Expand Up @@ -216,8 +216,12 @@ def _parse_size(self, size_str: str) -> int:
async def alist_packages(self) -> list[str]:
return (await self.aexec("uv pip list", kernel="bash")).text.splitlines()

async def alist_variables(self) -> list[str]:
return (await self.aexec("%who")).text.splitlines()
async def ashow_variables(self) -> dict[str, str]:
vars = [
line.strip() for line in (await self.aexec("%who")).text.strip().split("\t")
]
# todo remove that splitting thing when Out[0] thing is fixed
return {v: (await self.aexec(v)).text for v in vars}

async def arestart(self) -> None:
"""Restart the Jupyter kernel"""
Expand Down Expand Up @@ -253,8 +257,8 @@ def list_files(self) -> list[CodeBoxFile]:
def list_packages(self) -> list[str]:
return syncify(self.alist_packages)()

def list_variables(self) -> list[str]:
return syncify(self.alist_variables)()
def show_variables(self) -> dict[str, str]:
return syncify(self.ashow_variables)()

def restart(self) -> None:
return syncify(self.arestart)()
Expand Down
Loading

0 comments on commit f99eb64

Please sign in to comment.