Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pyodide notebooks support #3603

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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;
}
Loading