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

Documentation: Custom Widgets – Passing URLs #3338

Merged
merged 1 commit into from
Feb 8, 2022
Merged
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
49 changes: 47 additions & 2 deletions docs/source/examples/Widget Custom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,51 @@
"![end-result](./images/custom-widget-result.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Passing URLs\n",
"\n",
"In the example above we have seen how to pass simple unicode strings to a HTML input element. However,\n",
"certain HTML elements, like e.g. `<img/>`, `<iframe/>` or `<script/>` require URLs as input. Consider\n",
"a widget embedding an `<iframe/>`. The widget has a `src` property that is connected to the `src`\n",
"attribute of the `<iframe/>`. It is the ipywidget version of the built-in `IPython.display.IFrame(...)`.\n",
"Like the built-in we'd like to support two forms:\n",
"\n",
"```python\n",
"from ipyiframe import IFrame\n",
"\n",
"remote_url = IFrame(src='https://jupyter.org') # full HTTP URL\n",
"local_file = IFrame(src='./local_file.html') # local file\n",
"```\n",
"\n",
"Note, that the _second_ form is a path relative to the notebook file. Using this string as the `src`\n",
"attribute of the `<iframe/>` is not going to work, because the browser will interpret it as a relative\n",
"URL, relative to the browsers address bar. To transform the relative path into a valid file URL we use\n",
"the utility funtion `resolveUrl(...)` in our javascript view class:\n",
"\n",
"```js\n",
"export class IFrameView extends DOMWidgetView {\n",
" render() {\n",
" this.$iframe = document.createElement('iframe');\n",
" this.el.appendChild(this.$iframe);\n",
" this.src_changed();\n",
" this.model.on('change:src', this.src_changed, this);\n",
" }\n",
"\n",
" src_changed() {\n",
" const url = this.model.get('src'); \n",
" this.model.widget_manager.resolveUrl(url).then(resolvedUrl => { \n",
" this.$iframe.src = resolvedUrl;\n",
" }); \n",
" }\n",
"}\n",
"```\n",
"\n",
"Invoking `this.model.widget_manager.resolveUrl(...)` returns a promise that resolves to the correct URL."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -803,7 +848,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -817,7 +862,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.9.9"
}
},
"nbformat": 4,
Expand Down