Skip to content

Instantly share code, notes, and snippets.

@OliverBalfour
Last active May 19, 2024 11:38
Show Gist options
  • Save OliverBalfour/0f042848708d51ba698bb134e8c7c28e to your computer and use it in GitHub Desktop.
Save OliverBalfour/0f042848708d51ba698bb134e8c7c28e to your computer and use it in GitHub Desktop.
HTML custom elements for Jupyter notebooks.
"""
Paste this in your notebook after saving the HTML file in the same folder
Tested in VSCode notebooks, Google Colab and Jupyter Lab in Chrome
"""
from IPython.display import HTML
HTML(open("widget.html").read())
<script type='module'>
import {LitElement, html} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js';
export class CustomWidget extends LitElement {
static properties = {
checked: {},
};
constructor() {
super();
this.checked = false;
}
render() {
return html`
<div>
<input type="text" ?disabled=${!this.checked} value="Hello there.">
</div>
<label><input type="checkbox" @change=${
this.setChecked
}> Enable editing</label>
`;
}
setChecked(event) {
this.checked = event.target.checked;
}
}
customElements.define('custom-widget', CustomWidget);
</script>
<custom-widget />
<style>
body {
font-family: 'Open Sans', sans-serif;
font-size: 1.5em;
padding-left: 0.5em;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment