Skip to content

Instantly share code, notes, and snippets.

@lbustelo
Created November 23, 2015 15:50
Show Gist options
  • Save lbustelo/e32932cff1612464c38c to your computer and use it in GitHub Desktop.
Save lbustelo/e32932cff1612464c38c to your computer and use it in GitHub Desktop.
Example of scenario for jupyter/declarativewidgets#125
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython.display import HTML, Javascript, display"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is the first function. In this function we are using `display` to send javascript code that will call the `invoke` method of another `urth-core-function` element."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#First function\n",
"def function1(name):\n",
" display(HTML(\"<script>console.log('Invoking from kernel'); f2.invoke();</script>\"))\n",
" return 'Hello ' + name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%html\n",
"<template is=\"urth-core-bind\">\n",
" <urth-core-function id=\"f1\" ref=\"function1\" arg-name=\"{{the_name}}\" result=\"{{hello}}\" auto/>\n",
" the_name: <input type=\"text\" value=\"{{the_name::input}}\"></input>\n",
" <span>{{hello}}</span>\n",
"</template>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def function2(name):\n",
" return 'Goodbye ' + name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%html\n",
"<template is=\"urth-core-bind\">\n",
" <urth-core-function id=\"f2\" ref=\"function2\" arg-name=\"{{the_name}}\" result=\"{{goodbye}}\"/>\n",
" <span>{{goodbye}}</span>\n",
"</template>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### STOP Exetuting cells\n",
"Notice that entering data on the form above is **NOT** causing the 2nd function to get called. The `<urth-core-function id=\"f2\">` does not have `auto` set on purpose."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To prove that it might be the way we are invoking the python function when calling it through the `urth-core-function` element, calling the `function1` directly or evaluating a cell with `display` will actually cause `function2` to get called. **Remember to change the text on the input filed above** "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"display(HTML(\"<script>console.log('Invoking from kernel'); f2.invoke();</script>\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function1('some name')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment