Last active
May 19, 2024 11:38
-
-
Save OliverBalfour/0f042848708d51ba698bb134e8c7c28e to your computer and use it in GitHub Desktop.
HTML custom elements for Jupyter notebooks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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