Skip to content

Instantly share code, notes, and snippets.

@technic
Created February 8, 2019 12:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technic/a0b6f26322126f98d00b788e00d2cbb8 to your computer and use it in GitHub Desktop.
Save technic/a0b6f26322126f98d00b788e00d2cbb8 to your computer and use it in GitHub Desktop.
jupyter magic to manipulate notebook tags
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-02-08T12:29:36.689063Z",
"start_time": "2019-02-08T12:29:36.676068Z"
}
},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
"// If you want to execute the cell for the second time\n",
"require.undef('setTags');\n",
"\n",
"define('setTags', function() {\n",
" return function(element, tags) {\n",
" var cell_element = element.parents('.cell');\n",
" var index = Jupyter.notebook.get_cell_elements().index(cell_element);\n",
" var cell = Jupyter.notebook.get_cell(index);\n",
" cell.metadata.tags = tags;\n",
" }\n",
"});\n"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%javascript\n",
"\n",
"// If you want to execute the cell for the second time\n",
"require.undef('setTags');\n",
"\n",
"define('setTags', function() {\n",
" return function(element, tags) {\n",
" var cell_element = element.parents('.cell');\n",
" var index = Jupyter.notebook.get_cell_elements().index(cell_element);\n",
" var cell = Jupyter.notebook.get_cell(index);\n",
" cell.metadata.tags = tags;\n",
" }\n",
"});"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2019-02-08T12:29:36.711066Z",
"start_time": "2019-02-08T12:29:36.693061Z"
}
},
"outputs": [],
"source": [
"from IPython.core.magic import register_cell_magic, register_line_cell_magic\n",
"from IPython.display import Javascript, display\n",
"import json\n",
"\n",
"def _set_tags(tags):\n",
" assert all(map(lambda t: isinstance(t, str), tags))\n",
" display(Javascript(\n",
" \"\"\"\n",
" require(['setTags'], function(setTags) {\n",
" setTags(element, %s);\n",
" });\n",
" \"\"\" % json.dumps(tags)\n",
" ))\n",
"\n",
"@register_line_cell_magic\n",
"def tag(line, cell=None):\n",
" _set_tags(line.split())\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2019-02-08T12:29:36.733068Z",
"start_time": "2019-02-08T12:29:36.714063Z"
},
"tags": [
"code"
]
},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" require(['setTags'], function(setTags) {\n",
" setTags(element, [\"code\"]);\n",
" });\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world!\n"
]
}
],
"source": [
"%tag code\n",
"print('Hello world!')"
]
}
],
"metadata": {
"celltoolbar": "Tags",
"gist": {
"data": {
"description": "set-tags-magic.ipynb",
"public": true
},
"id": ""
},
"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.6.8"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@SultanOrazbayev
Copy link

This works in jupyter notebook, but not in jupyter lab. In jupyter lab I see "require is not defined" error message (but it works just fine in the jupyter notebook).

Do you know if there's a way to get the same magic in jupyter lab?

Here's a couple of relevant threads: Calysto/calysto_processing#11
jupyter-widgets/ipywidgets#2379

@technic
Copy link
Author

technic commented Nov 14, 2019

@SultanOrazbayev, unfortunately I don't have a jupyter lab solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment