Skip to content

Instantly share code, notes, and snippets.

@So-Cool
Created June 29, 2017 13:09
Show Gist options
  • Save So-Cool/c581fc7646d9cca18203812f5071d647 to your computer and use it in GitHub Desktop.
Save So-Cool/c581fc7646d9cca18203812f5071d647 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```bash\n",
"jupyter nbextension enable --py --sys-prefix widgetsnbextension\n",
"```\n",
"has to be run first"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
"import ipywidgets as widgets\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def f(x):\n",
" #return x\n",
" print \"test:\", x"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "207663d5a1c3431e8ecd9efa19109397"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(f, x=10);"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ef824a03b6be4d79a69717171a34b969"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(f, x=1.0);"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a12740b2acae47d8b48a62037ce6cc2d"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(f, x='Hi there!');"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "925d500f9f4744028f7b505ac677d443"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(f, x=True, description=\"sadf\");"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d75f3827be074386b0788e22e75694fc"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@interact(x=True, y=1.0, z=\"Test\")\n",
"def g(x, y, z):\n",
" return (x, y)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def h(p, q):\n",
" return (p, q)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b4dfd93f8521491791487bdd1fd56686"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(h, p=5, q=fixed(20));"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e123f8568c8d42a2a11b244a9c475349"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(f, x=widgets.IntSlider(min=-5,max=12,step=1,value=10));"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c94e3abe66854b8e946b5eab2abb1d4d"
}
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "67b896c80828462da332edeac0005229"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interact(f, x=['orange','apple']);\n",
"interact(f, x={'one':1,'two':2});"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "74ea4067f97640ebaf2a46c88c883c89"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# min, max, step\n",
"interact(f, x=(0.0,10.0,0.01));"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"w = interactive(h, p=10, q=20)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3621b1fd947a431c90373a9be32eb7f4"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(w)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3621b1fd947a431c90373a9be32eb7f4"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(w)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c90cf94219504c5faa553bcf1baa99f7"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# The interact_manual function provides a variant of interaction that allows you to restrict execution\n",
"# so it is only done on demand. A button is added to the interact controls that allows you to trigger\n",
"# an execute event.\n",
"\n",
"interact_manual(f, x=widgets.FloatSlider(min=1e5, max=1e7, step=1e5));"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "41b9917ae8e6432a9bd578f65028e724"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# If you are using slider widgets, you can set the continuous_update kwarg to False.\n",
"#continuous_update is a kwarg of slider widgets that restricts executions to mouse release events.\n",
"\n",
"interact(f, x=widgets.FloatSlider(min=1e5, max=1e7, step=1e5, continuous_update=False));"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b184cc73134f495e987d7442539cadac"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x_widget = widgets.FloatSlider(min=0.0, max=10.0, step=0.05)\n",
"y_widget = widgets.FloatSlider(min=0.5, max=10.0, step=0.05, value=5.0)\n",
"\n",
"def update_x_range(*args):\n",
" x_widget.max = 2.0 * y_widget.value\n",
"y_widget.observe(update_x_range, 'value')\n",
"\n",
"def printer(x, y):\n",
" print x, y\n",
"interact(printer,x=x_widget, y=y_widget);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment