Skip to content

Commit

Permalink
🔨 initial sketch of notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Oct 24, 2024
1 parent 19e2195 commit 6a37368
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
53 changes: 52 additions & 1 deletion packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ import {
type EntitySelectorState,
} from "../entitySelector/EntitySelector"
import { SlideInDrawer } from "../slideInDrawer/SlideInDrawer"

declare const StarboardEmbed: any // Inform TypeScript about StarboardEmbed

Check warning on line 213 in packages/@ourworldindata/grapher/src/core/Grapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

'StarboardEmbed' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 213 in packages/@ourworldindata/grapher/src/core/Grapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

'StarboardEmbed' is defined but never used. Allowed unused vars must match /^_/u
declare global {
interface Window {
details?: DetailDictionary
Expand Down Expand Up @@ -2371,6 +2371,49 @@ export class Grapher
void this.timelineController.togglePlay()
}

@action.bound private async addNotebook() {

Check warning on line 2374 in packages/@ourworldindata/grapher/src/core/Grapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Missing return type on function

Check warning on line 2374 in packages/@ourworldindata/grapher/src/core/Grapher.tsx

View workflow job for this annotation

GitHub Actions / eslint

Missing return type on function
const notebookNode = document.createElement("div")
notebookNode.id = "notebook"
document.body.appendChild(notebookNode)
const packageUrl = "https://unpkg.com/starboard-wrap/dist/index.js"

const module = await import(packageUrl)

// Ensure the module is available and extract the necessary export
const { StarboardEmbed } = module

// Create a script tag to load the necessary library
const notebookContent = `# %% [markdown]
# Setup code
The code in the cell below provides the infrastructure to access our data. It is automatically run when the page is loaded.
# %%--- [python]
# properties:
# run_on_load: true
# ---%%
from pyodide.http import open_url
import pandas as pd
csv = open_url("${this.baseUrl}.csv")
df = pd.read_csv(csv)
# %% [markdown]
Add your code below - the example shows the first few rows of the data. Refer to the pandas documentation of ask ChatGPT for help on how to manipulate pandas dataframes.
# %%--- [python]
# properties:
# run_on_load: true
# ---%%
df.head()
`

const mount = document.querySelector("#notebook")
const el = new StarboardEmbed({
notebookContent: notebookContent,
src: "https://unpkg.com/starboard-notebook/dist/index.html",
})

mount!.appendChild(el)
}

selection =
this.manager?.selection ??
new SelectionArray(
Expand Down Expand Up @@ -2472,6 +2515,14 @@ export class Grapher
title: "Reset to original",
category: "Navigation",
},
{
combo: "ctrl+n",
fn: (): void => {
void this.addNotebook()
},
title: "Open a notebook",
category: "Notebook",
},
]

if (this.slideShow) {
Expand Down
11 changes: 11 additions & 0 deletions packages/@ourworldindata/grapher/src/core/grapher.scss
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,14 @@ $zindex-controls-drawer: 150;
.dod-container {
@include dod-container;
}

#notebook {
position: absolute;
top: 70px;
left: 50px;
right: 50px;
bottom: 50px;
background: white;
z-index: 100;
overflow-y: scroll;
}

0 comments on commit 6a37368

Please sign in to comment.