Skip to content

Commit

Permalink
Add text method
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Mar 29, 2024
1 parent e0d7364 commit 7530653
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 22 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ xlrd==2.0.1
### Custom template using Code Interpreter

The template requires custom setup. If you want to build your own custom template and use Code Interpreter, you need to do:

1. Copy `jupyter_server_config.py` and `start-up.sh` from this PR
2. Add following commands in your Dockerfile

```Dockerfile
# Installs jupyter server and kernel
RUN pip install jupyter-server ipykernel ipython
Expand All @@ -226,7 +228,9 @@ COPY ./jupyter_server_config.py /home/user/.jupyter/
COPY ./start-up.sh /home/user/.jupyter/
RUN chmod +x /home/user/.jupyter/start-up.sh
```
3. Add the following option `-c "/home/user/.jupyter/start-up.sh"` to `e2b template build` command or add this line to your `e2b.toml`.

3. Add the following option `-c "/home/user/.jupyter/start-up.sh"` to `e2b template build` command or add this line to your `e2b.toml`.

```yaml
start_cmd = "/home/user/.jupyter/start-up.sh"
```
8 changes: 6 additions & 2 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const sandbox = await CodeInterpreter.create()
await sandbox.notebook.execCell('x = 1')

const result = await sandbox.notebook.execCell('x += 1; x')
console.log(result.output) // outputs 2
console.log(result.text) // outputs 2

await sandbox.close()
```
Expand Down Expand Up @@ -125,8 +125,10 @@ xlrd==2.0.1
### Custom template using Code Interpreter

The template requires custom setup. If you want to build your own custom template and use Code Interpreter, you need to do:

1. Copy `jupyter_server_config.py` and `start-up.sh` from this PR
2. Add following commands in your Dockerfile

```Dockerfile
# Installs jupyter server and kernel
RUN pip install jupyter-server ipykernel ipython
Expand All @@ -137,7 +139,9 @@ COPY ./jupyter_server_config.py /home/user/.jupyter/
COPY ./start-up.sh /home/user/.jupyter/
RUN chmod +x /home/user/.jupyter/start-up.sh
```
3. Add the following option `-c "/home/user/.jupyter/start-up.sh"` to `e2b template build` command or add this line to your `e2b.toml`.

3. Add the following option `-c "/home/user/.jupyter/start-up.sh"` to `e2b template build` command or add this line to your `e2b.toml`.

```yaml
start_cmd = "/home/user/.jupyter/start-up.sh"
```
25 changes: 13 additions & 12 deletions js/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ export interface DisplayData {
* @property {string[]} stderr - List of strings printed to stderr by prints, subprocesses, etc.
* @property {Error | null} error - An Error object if an error occurred, null otherwise.
*/
export interface Cell {
result: DisplayData
displayData: DisplayData[]
stdout: string[]
stderr: string[]
error?: Error
export class Cell {
constructor(
public result: DisplayData,
public displayData: DisplayData[],
public stdout: string[],
public stderr: string[],
public error?: Error
) {}

public text() {
return this.result['text/plain']
}
}

/**
Expand All @@ -50,12 +56,7 @@ class CellExecution {
onStdout?: (out: ProcessMessage) => Promise<void> | void,
onStderr?: (out: ProcessMessage) => Promise<void> | void
) {
this.result = {
stdout: [],
stderr: [],
displayData: [],
result: {}
}
this.result = new Cell({}, [], [], [])
this.onStdout = onStdout
this.onStderr = onStderr
}
Expand Down
2 changes: 1 addition & 1 deletion js/tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test('basic', async () => {

const output = await sandbox.notebook.execCell('x =1; x')

expect(output.result['text/plain']).toEqual('1')
expect(output.text).toEqual('1')

await sandbox.close()
})
2 changes: 1 addition & 1 deletion js/tests/statefulness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('statefulness', async () => {

const output = await sandbox.notebook.execCell('x += 1; x')

expect(output.result['text/plain']).toEqual('2')
expect(output.text).toEqual('2')

await sandbox.close()
})
2 changes: 1 addition & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ with CodeInterpreter() as sandbox:
sandbox.exec_cell("x = 1")

result = sandbox.exec_cell("x += 1; x")
print(result.result) # outputs 2
print(result.text) # outputs 2

```

Expand Down
3 changes: 1 addition & 2 deletions python/e2b_code_interpreter/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Dict, List, Optional
from typing_extensions import TypedDict
from pydantic import BaseModel


Expand Down Expand Up @@ -53,7 +52,7 @@ def text(self) -> str:
:return: The text representation of the result.
"""
return self.result["text/plain"]
return self.result.get("text/plain", None)


class KernelException(Exception):
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
def test_basic():
with CodeInterpreter() as sandbox:
result = sandbox.notebook.exec_cell("x =1; x")
assert result.result["text/plain"] == "1"
assert result.text == "1"
2 changes: 1 addition & 1 deletion python/tests/test_statefulness.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ def test_stateful():
sandbox.notebook.exec_cell("x = 1")

result = sandbox.notebook.exec_cell("x+=1; x")
assert result.result["text/plain"] == "2"
assert result.text == "2"

0 comments on commit 7530653

Please sign in to comment.