Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Run Python code from JavaScript in a Jupyter notebook
// %%javascript
window.executePython = function(python) {
return new Promise((resolve, reject) => {
var callbacks = {
iopub: {
output: (data) => resolve(data.content.text.trim())
Jupyter.notebook.kernel.execute(`print(${python})`, callbacks);
// Use it in any Jupyter JS/HTML cell like this
window.executePython("1 + 1")
.then(result => console.log(result)); // Logs 2
// You can access any defined object/method in the notebook
// I suggest writing a function that returns your data as JSON and just calling the function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.