{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "custom_js = \"\"\"\n", "// nbextensions snippets menu customization.\n", "var json_data;\n", "$.getJSON( \"python_snippets.json\", function(data) { \n", " json_data = data;\n", "});\n", "\n", "require([\"nbextensions/snippets_menu/main\"], function (snippets_menu) {\n", " console.log('Loading `snippets_menu` customizations from `custom.js`');\n", " var horizontal_line = '---';\n", " var snippets_json = json_data\n", " snippets_menu.options['menus'] = snippets_menu.default_menus;\n", " snippets_menu.options['menus'][0]['sub-menu'].push(horizontal_line);\n", " snippets_menu.options['menus'][0]['sub-menu'].push(snippets_json);\n", " console.log('Loaded `snippets_menu` customizations from `custom.js`');\n", "});\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import os\n", "import glob\n", "import re\n", "\n", "home = os.path.expanduser(\"~\")\n", "jupyter_home = os.path.join(home,'.jupyter')\n", "jupyter_custom = os.path.join(jupyter_home, 'custom')\n", "test_str = '// nbextensions snippets menu customization.'\n", "\n", "if 'custom' in os.listdir(jupyter_home):\n", " file_names = list(os.walk(jupyter_custom))[0][-1]\n", " custom_js_file = os.path.join(jupyter_custom,'custom.js')\n", " if 'custom.js' in file_names:\n", " # Get the contents of the custom.js file.\n", " f = open(custom_js_file, 'r')\n", " contents = f.read()\n", " f.close()\n", " # Test to see if the js code has been added.\n", " if test_str in contents:\n", " pass\n", " # If the code hasn't been added then add it.\n", " else:\n", " f = open(custom_js_file, 'a+')\n", " f.write(custom_js)\n", " f.close()\n", " # If there is not custom.js file create one and write code to it.\n", " else:\n", " f = open(custom_js_file, 'w')\n", " f.write(custom_js)\n", " f.close()\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "ent =dict()\n", "ent['name'] = 'Boiler-plate'\n", "ent['snippet'] = ['import numpy as np','import pandas as pd']\n", "\n", "d = dict()\n", "d['name'] = 'Basic'\n", "d['sub-menu'] = [ent]\n", "\n", "json_file = json.dumps(d)\n", "\n", "# json_file_name = os.path.join(jupyter_custom, 'python_snippets.json')\n", "# f = open(json_file_name, 'w')\n", "\n", "f = open('python_snippets.json', 'w')\n", "f.write(json_file)\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", "// $.getJSON( \"json_practice.json\", function( data ) { \n", "// // console.log( \"JSON Data: \" + data);\n", "// $.each( data, function( key, val ) {\n", "// // console.log(key + \"value:: \" + val );\n", "// var json_data = key + \"value:: \" + val;\n", "// var cell = Jupyter.notebook.insert_cell_below('markdown');\n", "// cell.set_text(data);\n", "// cell.render();\n", "// });\n", "// });" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "\n", "// $.getJSON( \"json_practice.json\", function( data ) { \n", "// // console.log( \"JSON Data: \" + data);\n", "// $.each( data, function( key, val ) {\n", "// // console.log(key + \"value:: \" + val );\n", "// var json_data = key + \"value:: \" + val;\n", "// var cell = Jupyter.notebook.insert_cell_below('markdown');\n", "// cell.set_text(data);\n", "// cell.render();\n", "// });\n", "// });\n", "\n", "\n", "// $.getJSON( \"json_practice.json\", function(data) { \n", "// console.log(data['name']);\n", "// });\n", "\n", "\n", "// var my_fav;\n", "// $.getJSON( \"json_practice.json\", function(data) { \n", "// my_fav = data;\n", "// });" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "// nbextensions snippets menu customization.\n", "var my_fav;\n", "$.getJSON( \"python_snippets.json\", function(data) { \n", " my_fav = data;\n", "});\n", "\n", "require([\"nbextensions/snippets_menu/main\"], function (snippets_menu) {\n", " console.log('Loading `snippets_menu` customizations from `custom.js`');\n", " var horizontal_line = '---';\n", " var my_favorites = my_fav\n", " snippets_menu.options['menus'] = snippets_menu.default_menus;\n", " snippets_menu.options['menus'][0]['sub-menu'].push(horizontal_line);\n", " snippets_menu.options['menus'][0]['sub-menu'].push(my_favorites);\n", " console.log('Loaded `snippets_menu` customizations from `custom.js`');\n", "});" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "// nbextensions snippets menu customization.\n", "var my_fav;\n", "$.getJSON( \"python_snippets.json\", function(data) { \n", " my_fav = data;\n", "});\n", "\n", "require([\"nbextensions/snippets_menu/main\"], function (snippets_menu) {\n", " console.log('Loading `snippets_menu` customizations from `custom.js`');\n", " var horizontal_line = '---';\n", " var my_favorites = my_fav\n", " snippets_menu.options['menus'] = snippets_menu.default_menus;\n", " snippets_menu.options['menus'][0]['sub-menu'].push(horizontal_line);\n", " snippets_menu.options['menus'][0]['sub-menu'].push(my_favorites);\n", " console.log('Loaded `snippets_menu` customizations from `custom.js`');\n", "});\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "var ci = Jupyter.notebook.get_selected_cell().input;\n", "var txt = ci.contents()[1].innerText;\n", "console.log(txt);" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "// Get the contents of your selected cell and loag it to console.\n", "var ci = Jupyter.notebook.get_selected_cell().input;\n", "var txt = ci.contents()[1].innerText;\n", "console.log(txt);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.6.1" }, "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 }