Skip to content

Commit

Permalink
Rename to chart
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Oct 15, 2024
1 parent d1f419c commit 8666df5
Show file tree
Hide file tree
Showing 36 changed files with 457 additions and 463 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
paths:
- 'template/**'
- '.github/workflows/template.yml'
branches:
- beta

permissions:
contents: read
Expand Down
2 changes: 1 addition & 1 deletion js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The repository contains a template and modules for the code interpreter sandbox.
## Key Features

- **Stateful Execution**: Unlike traditional sandboxes that treat each code execution independently, this package maintains context across executions.
- **Displaying Graph & Data**: Implements parts of the [Jupyter Kernel messaging protocol](https://jupyter-client.readthedocs.io/en/latest/messaging.html), which support for interactive features like plotting charts, rendering DataFrames, etc.
- **Displaying Charts & Data**: Implements parts of the [Jupyter Kernel messaging protocol](https://jupyter-client.readthedocs.io/en/latest/messaging.html), which support for interactive features like plotting charts, rendering DataFrames, etc.

## Installation

Expand Down
86 changes: 43 additions & 43 deletions js/src/graphs.ts → js/src/charts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Graph types
* Chart types
*/
export enum GraphType {
export enum ChartType {
LINE = 'line',
SCATTER = 'scatter',
BAR = 'bar',
PIE = 'pie',
BOX_AND_WHISKER = 'box_and_whisker',
SUPERGRAPH = 'supergraph',
SUPERCHART = 'superchart',
UNKNOWN = 'unknown',
}

Expand All @@ -27,13 +27,13 @@ export enum ScaleType {
ASINH = "asinh",
}

export type Graph = {
type: GraphType
export type Chart = {
type: ChartType
title: string
elements: any[]
}

type Graph2D = Graph & {
type Chart2D = Chart & {
x_label?: string
y_label?: string
x_unit?: string
Expand All @@ -45,7 +45,7 @@ export type PointData = {
points: [number | string, number | string][]
}

type PointGraph = Graph2D & {
type PointChart = Chart2D & {
x_ticks: (number | string)[]
x_scale: ScaleType
x_tick_labels: string[]
Expand All @@ -55,12 +55,12 @@ type PointGraph = Graph2D & {
elements: PointData[]
}

export type LineGraph = PointGraph & {
type: GraphType.LINE
export type LineChart = PointChart & {
type: ChartType.LINE
}

export type ScatterGraph = PointGraph & {
type: GraphType.SCATTER
export type ScatterChart = PointChart & {
type: ChartType.SCATTER
}

export type BarData = {
Expand All @@ -69,8 +69,8 @@ export type BarData = {
group: string
}

export type BarGraph = Graph2D & {
type: GraphType.BAR
export type BarChart = Chart2D & {
type: ChartType.BAR
elements: BarData[]
}

Expand All @@ -80,8 +80,8 @@ export type PieData = {
radius: number
}

export type PieGraph = Graph & {
type: GraphType.PIE
export type PieChart = Chart & {
type: ChartType.PIE
elements: PieData[]
}

Expand All @@ -94,43 +94,43 @@ export type BoxAndWhiskerData = {
max: number
}

export type BoxAndWhiskerGraph = Graph2D & {
type: GraphType.BOX_AND_WHISKER
export type BoxAndWhiskerChart = Chart2D & {
type: ChartType.BOX_AND_WHISKER
elements: BoxAndWhiskerData[]
}

export type SuperGraph = Graph & {
type: GraphType.SUPERGRAPH
elements: Graph[]
export type SuperChart = Chart & {
type: ChartType.SUPERCHART
elements: Chart[]
}

export type GraphTypes =
| LineGraph
| ScatterGraph
| BarGraph
| PieGraph
| BoxAndWhiskerGraph
| SuperGraph
export function deserializeGraph(data: any): Graph {
export type ChartTypes =
| LineChart
| ScatterChart
| BarChart
| PieChart
| BoxAndWhiskerChart
| SuperChart
export function deserializeChart(data: any): Chart {
switch (data.type) {
case GraphType.LINE:
return { ...data } as LineGraph
case GraphType.SCATTER:
return { ...data } as ScatterGraph
case GraphType.BAR:
return { ...data } as BarGraph
case GraphType.PIE:
return { ...data } as PieGraph
case GraphType.BOX_AND_WHISKER:
return { ...data } as BoxAndWhiskerGraph
case GraphType.SUPERGRAPH:
const graphs = data.data.map((g: any) => deserializeGraph(g))
case ChartType.LINE:
return { ...data } as LineChart
case ChartType.SCATTER:
return { ...data } as ScatterChart
case ChartType.BAR:
return { ...data } as BarChart
case ChartType.PIE:
return { ...data } as PieChart
case ChartType.BOX_AND_WHISKER:
return { ...data } as BoxAndWhiskerChart
case ChartType.SUPERCHART:
const charts = data.data.map((g: any) => deserializeChart(g))
delete data.data
return {
...data,
data: graphs,
} as SuperGraph
data: charts,
} as SuperChart
default:
return { ...data, type: GraphType.UNKNOWN } as Graph
return { ...data, type: ChartType.UNKNOWN } as Chart
}
}
20 changes: 10 additions & 10 deletions js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ export type {
} from './messaging'
export type {
ScaleType,
GraphType,
GraphTypes,
Graph,
BarGraph,
ChartType,
ChartTypes,
Chart,
BarChart,
BarData,
LineGraph,
ScatterGraph,
BoxAndWhiskerGraph,
LineChart,
ScatterChart,
BoxAndWhiskerChart,
BoxAndWhiskerData,
PieGraph,
PieChart,
PieData,
SuperGraph,
SuperChart,
PointData,
} from './graphs'
} from './charts'
import { Sandbox } from './sandbox'

export default Sandbox
10 changes: 5 additions & 5 deletions js/src/messaging.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NotFoundError, SandboxError, TimeoutError } from 'e2b'
import { GraphTypes } from './graphs'
import { ChartTypes } from './charts'

export async function extractError(res: Response) {
if (res.ok) {
Expand Down Expand Up @@ -61,7 +61,7 @@ export type MIMEType = string

type E2BData = {
data: Record<string, unknown>
graph: GraphTypes
chart: ChartTypes
}

/**
Expand Down Expand Up @@ -126,9 +126,9 @@ export class Result {
*/
readonly data?: Record<string, unknown>
/**
* Contains the graph data.
* Contains the chart data.
*/
readonly graph?: GraphTypes
readonly chart?: ChartTypes
/**
* Extra data that can be included. Not part of the standard types.
*/
Expand All @@ -155,7 +155,7 @@ export class Result {
this.raw = data

this.data = data['data']
this.graph = data['graph']
this.chart = data['chart']

this.extra = {}

Expand Down
22 changes: 11 additions & 11 deletions js/tests/graphs/bar.test.ts → js/tests/charts/bar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ import matplotlib.pyplot as plt
authors = ['Author A', 'Author B', 'Author C', 'Author D']
sales = [100, 200, 300, 400]
# Create and customize the bar graph
# Create and customize the bar chart
plt.figure(figsize=(10, 6))
plt.bar(authors, sales, label='Books Sold', color='blue')
plt.xlabel('Authors')
plt.ylabel('Number of Books Sold')
plt.title('Book Sales by Authors')
# Display the graph
# Display the chart
plt.tight_layout()
plt.show()
`
const result = await sandbox.runCode(code)
const graph = result.results[0].graph
const chart = result.results[0].chart

expect(graph).toBeDefined()
expect(graph.type).toBe('bar')
expect(graph.title).toBe('Book Sales by Authors')
expect(chart).toBeDefined()
expect(chart.type).toBe('bar')
expect(chart.title).toBe('Book Sales by Authors')

expect(graph.x_label).toBe('Authors')
expect(graph.y_label).toBe('Number of Books Sold')
expect(chart.x_label).toBe('Authors')
expect(chart.y_label).toBe('Number of Books Sold')

expect(graph.x_unit).toBeNull()
expect(graph.y_unit).toBeNull()
expect(chart.x_unit).toBeNull()
expect(chart.y_unit).toBeNull()

const bars = graph.elements
const bars = chart.elements
expect(bars.length).toBe(4)

expect(bars.map((bar) => bar.value)).toEqual([100, 200, 300, 400])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ plt.tight_layout()
plt.show()
`
const result = await sandbox.runCode(code)
const graph = result.results[0].graph
const chart = result.results[0].chart

expect(graph).toBeDefined()
expect(chart).toBeDefined()

expect(graph.type).toBe('box_and_whisker')
expect(graph.title).toBe('Exam Scores Distribution')
expect(chart.type).toBe('box_and_whisker')
expect(chart.title).toBe('Exam Scores Distribution')

expect(graph.x_label).toBe('Class')
expect(graph.y_label).toBe('Score')
expect(chart.x_label).toBe('Class')
expect(chart.y_label).toBe('Score')

expect(graph.x_unit).toBeNull()
expect(graph.y_unit).toBeNull()
expect(chart.x_unit).toBeNull()
expect(chart.y_unit).toBeNull()

const bars = graph.elements
const bars = chart.elements
expect(bars.length).toBe(3)

bars.forEach((bar: any) => {
Expand Down
32 changes: 16 additions & 16 deletions js/tests/graphs/line.test.ts → js/tests/charts/line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ plt.title('Plot of sin(x) and cos(x)')
plt.show()
`
const result = await sandbox.runCode(code)
const graph = result.results[0].graph
const chart = result.results[0].chart

expect(graph).toBeDefined()
expect(graph.type).toBe('line')
expect(chart).toBeDefined()

Check failure on line 35 in js/tests/charts/line.test.ts

View workflow job for this annotation

GitHub Actions / Tests JS package / Build and test SDK

tests/charts/line.test.ts > line

AssertionError: expected undefined not to be undefined ❯ tests/charts/line.test.ts:35:17
expect(chart.type).toBe('line')

expect(graph.title).toBe('Plot of sin(x) and cos(x)')
expect(graph.x_label).toBe('Time (s)')
expect(graph.y_label).toBe('Amplitude (Hz)')
expect(chart.title).toBe('Plot of sin(x) and cos(x)')
expect(chart.x_label).toBe('Time (s)')
expect(chart.y_label).toBe('Amplitude (Hz)')

expect(graph.x_scale).toBe('datetime')
expect(graph.y_scale).toBe('linear')
expect(chart.x_scale).toBe('datetime')
expect(chart.y_scale).toBe('linear')

expect(graph.x_unit).toBe('s')
expect(graph.y_unit).toBe('Hz')
expect(chart.x_unit).toBe('s')
expect(chart.y_unit).toBe('Hz')

expect(graph.x_ticks.every((tick: number) => typeof tick === 'string')).toBe(
expect(chart.x_ticks.every((tick: number) => typeof tick === 'string')).toBe(
true,
)
expect(new Date(graph.x_ticks[0])).toBeInstanceOf(Date)
expect(graph.y_ticks.every((tick: number) => typeof tick === 'number')).toBe(
expect(new Date(chart.x_ticks[0])).toBeInstanceOf(Date)
expect(chart.y_ticks.every((tick: number) => typeof tick === 'number')).toBe(
true,
)

expect(
graph.y_tick_labels.every((label: string) => typeof label === 'string'),
chart.y_tick_labels.every((label: string) => typeof label === 'string'),
).toBe(true)
expect(
graph.x_tick_labels.every((label: string) => typeof label === 'string'),
chart.x_tick_labels.every((label: string) => typeof label === 'string'),
).toBe(true)

const lines = graph.elements
const lines = chart.elements
expect(lines.length).toBe(2)

const [firstLine, secondLine] = lines
Expand Down
Loading

0 comments on commit 8666df5

Please sign in to comment.