Skip to content

Commit cba391f

Browse files
committed
Add scale
1 parent fe5db6c commit cba391f

12 files changed

Lines changed: 161 additions & 13 deletions

File tree

File renamed without changes.

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"semi": false,
4+
"trailingComma": "es5"
5+
}

js/.prettierrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

js/src/graphs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ type Graph2D = Graph & {
2323

2424
export type PointData = {
2525
label: string
26-
points: [(number| string), (number | string)][]
26+
points: [number | string, number | string][]
2727
}
2828

2929
type PointGraph = Graph2D & {
3030
x_ticks: (number | string)[]
31+
x_scale: string
3132
x_tick_labels: string[]
3233
y_ticks: (number | string)[]
34+
y_scale: string
3335
y_tick_labels: string[]
3436
elements: PointData[]
3537
}

js/tests/envVars.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ sandboxTest('env vars on sandbox override', async () => {
4444
expect(result2.results[0].text.trim()).toEqual('runtime')
4545

4646
if (!isDebug) {
47-
const result3 = await sandbox.notebook.execCell("import os; os.getenv('SBX')")
47+
const result3 = await sandbox.notebook.execCell(
48+
"import os; os.getenv('SBX')"
49+
)
4850
expect(result3.results[0].text.trim()).toEqual('value')
4951
}
5052

js/tests/graphs/bar.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ plt.show()
3939
expect(bars.length).toBe(4)
4040

4141
expect(bars.map((bar) => bar.value)).toEqual([100, 200, 300, 400])
42-
expect(bars.map((bar) => bar.group)).toEqual(['Books Sold', 'Books Sold', 'Books Sold', 'Books Sold'])
42+
expect(bars.map((bar) => bar.group)).toEqual([
43+
'Books Sold',
44+
'Books Sold',
45+
'Books Sold',
46+
'Books Sold',
47+
])
4348
expect(bars.map((bar) => bar.label)).toEqual([
4449
'Author A',
4550
'Author B',

js/tests/graphs/line.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ plt.show()
6666
expect(
6767
firstLine.points.every(
6868
(point: [number, number]) =>
69-
typeof point[0] === "string" && typeof point[1] === 'number'
69+
typeof point[0] === 'string' && typeof point[1] === 'number'
7070
)
7171
).toBe(true)
72-
expect(new Date(firstLine.points[0][0])).toEqual(new Date('2023-09-01T00:00:00.000Z'))
72+
expect(new Date(firstLine.points[0][0])).toEqual(
73+
new Date('2023-09-01T00:00:00.000Z')
74+
)
7375

7476
expect(secondLine.label).toBe('cos(x)')
7577
expect(secondLine.points.length).toBe(100)

js/tests/graphs/log.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { expect } from 'vitest'
2+
3+
import { sandboxTest } from '../setup'
4+
5+
sandboxTest('log', async ({ sandbox }) => {
6+
const code = `
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
10+
# Generate x values
11+
x = np.linspace(0, 100, 100)
12+
# Calculate y values
13+
y = np.exp(x)
14+
15+
# Create the plot
16+
plt.figure(figsize=(10, 6))
17+
plt.plot(x, y, label='y = e^x')
18+
19+
# Set log scale for the y-axis
20+
plt.yscale('log')
21+
22+
# Add labels and title
23+
plt.xlabel('X-axis')
24+
plt.ylabel('Y-axis (log scale)')
25+
plt.title('Graph with Log Scale on Y-axis')
26+
27+
plt.legend()
28+
plt.grid(True)
29+
plt.show()
30+
`
31+
32+
const result = await sandbox.notebook.execCell(code)
33+
const graph = result.results[0].graph
34+
expect(graph).toBeDefined()
35+
expect(graph.type).toBe('line')
36+
37+
expect(graph.title).toBe('Graph with Log Scale on Y-axis')
38+
39+
expect(graph.x_label).toBe('X-axis')
40+
expect(graph.y_label).toBe('Y-axis (log scale)')
41+
42+
expect(graph.x_unit).toBeNull()
43+
expect(graph.y_unit).toBe('log scale')
44+
45+
expect(graph.x_scale).toBe('linear')
46+
expect(graph.y_scale).toBe('log')
47+
48+
expect(graph.x_ticks.every((x) => typeof x === 'number')).toBe(true)
49+
expect(graph.y_ticks.every((y) => typeof y === 'number')).toBe(true)
50+
51+
expect(graph.x_tick_labels.every((x) => typeof x === 'string')).toBe(true)
52+
expect(graph.y_tick_labels.every((y) => typeof y === 'string')).toBe(true)
53+
54+
const lines = graph.elements
55+
expect(lines.length).toBe(1)
56+
57+
const line = lines[0]
58+
expect(line.label).toBe('y = e^x')
59+
expect(line.points.length).toBe(100)
60+
61+
expect(
62+
line.points.every(
63+
([x, y]) => typeof x === 'number' && typeof y === 'number'
64+
)
65+
).toBe(true)
66+
})

python/e2b_code_interpreter/graphs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ def __init__(self, **kwargs):
5050
class PointGraph(Graph2D):
5151
x_ticks: List[Union[str, int, float]]
5252
x_tick_labels: List[str]
53-
x_unit: Optional[str]
53+
x_scale: str
5454

5555
y_ticks: List[Union[str, int, float]]
5656
y_tick_labels: List[str]
57-
y_unit: Optional[str]
57+
y_scale: str
5858

5959
elements: List[PointData]
6060

6161
def __init__(self, **kwargs):
6262
super().__init__(**kwargs)
6363
self.x_label = kwargs["x_label"]
64-
self.x_unit = kwargs["x_unit"]
64+
self.x_scale = kwargs["x_scale"]
6565
self.x_ticks = kwargs["x_ticks"]
6666
self.x_tick_labels = kwargs["x_tick_labels"]
6767

6868
self.y_label = kwargs["y_label"]
69-
self.y_unit = kwargs["y_unit"]
69+
self.y_scale = kwargs["y_scale"]
7070
self.y_ticks = kwargs["y_ticks"]
7171
self.y_tick_labels = kwargs["y_tick_labels"]
7272

python/tests/graphs/test_line.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ async def test_line_graph(async_sandbox: AsyncCodeInterpreter):
4646
assert graph.x_unit == "s"
4747
assert graph.y_unit == "Hz"
4848

49+
assert graph.x_scale == "datetime"
50+
assert graph.y_scale == "linear"
51+
4952
assert all(isinstance(x, str) for x in graph.x_ticks)
5053
parsed_date = datetime.datetime.fromisoformat(graph.x_ticks[0])
5154
assert isinstance(parsed_date, datetime.datetime)

0 commit comments

Comments
 (0)