Skip to content

Instantly share code, notes, and snippets.

@nikhilkumarsingh
Created November 10, 2019 11:34
Show Gist options
  • Save nikhilkumarsingh/e17f01a8b68bc38e85f3f69ad6741f70 to your computer and use it in GitHub Desktop.
Save nikhilkumarsingh/e17f01a8b68bc38e85f3f69ad6741f70 to your computer and use it in GitHub Desktop.
Code formatting in Jupyter Notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Jupyter Notebook Code Formatter\n",
"\n",
"[nb_black](https://github.com/dnanhkhoa/nb_black)\n",
"\n",
"`pip install nb_black`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" setTimeout(function() {\n",
" var nbb_cell_id = 1;\n",
" var nbb_formatted_code = \"%load_ext nb_black\";\n",
" var nbb_cells = Jupyter.notebook.get_cells();\n",
" for (var i = 0; i < nbb_cells.length; ++i) {\n",
" if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n",
" nbb_cells[i].set_text(nbb_formatted_code);\n",
" break;\n",
" }\n",
" }\n",
" }, 500);\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%load_ext nb_black"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" setTimeout(function() {\n",
" var nbb_cell_id = 2;\n",
" var nbb_formatted_code = \"d = {\\\"a\\\": 1, \\\"b\\\": 2, \\\"c\\\": \\\"nikhil\\\"}\";\n",
" var nbb_cells = Jupyter.notebook.get_cells();\n",
" for (var i = 0; i < nbb_cells.length; ++i) {\n",
" if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n",
" nbb_cells[i].set_text(nbb_formatted_code);\n",
" break;\n",
" }\n",
" }\n",
" }, 500);\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"d = {\"a\": 1, \"b\":2, \n",
" 'c' : \"nikhil\"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Formatted:\n",
"\n",
"```python\n",
"d = {\"a\": 1, \"b\": 2, \"c\": \"nikhil\"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" setTimeout(function() {\n",
" var nbb_cell_id = 3;\n",
" var nbb_formatted_code = \"class bdist_rpm:\\n\\n user_options = [\\n (\\\"bdist-base=\\\", None, \\\"base directory for creating built distributions\\\"),\\n (\\n \\\"rpm-base=\\\",\\n None,\\n 'base directory for creating RPMs (defaults to \\\"rpm\\\" under '\\n \\\"--bdist-base; must be specified for RPM 2)\\\",\\n ),\\n (\\n \\\"dist-dir=\\\",\\n \\\"d\\\",\\n \\\"directory to put final RPM files in \\\" \\\"(and .spec files if --spec-only)\\\",\\n ),\\n (\\\"source-only\\\", None, \\\"only generate source RPM\\\"),\\n (\\\"binary-only\\\", None, \\\"only generate binary RPM\\\"),\\n ]\\n\\n def _format_changelog(self, changelog):\\n if not changelog:\\n return changelog\\n\\n new_changelog = []\\n for line in string.split(string.strip(changelog), \\\"\\\\n\\\"):\\n line = string.strip(line)\\n if line[0] == \\\"*\\\":\\n new_changelog.extend([\\\"\\\"])\\n elif line[0] == \\\"-\\\":\\n new_changelog.append(line)\\n else:\\n new_changelog.append(\\\" \\\" + line)\";\n",
" var nbb_cells = Jupyter.notebook.get_cells();\n",
" for (var i = 0; i < nbb_cells.length; ++i) {\n",
" if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n",
" nbb_cells[i].set_text(nbb_formatted_code);\n",
" break;\n",
" }\n",
" }\n",
" }, 500);\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"class bdist_rpm :\n",
"\n",
" user_options = [ \n",
" \n",
" ('bdist-base=', \n",
" None,\n",
" \"base directory for creating built distributions\"), ('rpm-base=', None,\n",
" \"base directory for creating RPMs (defaults to \\\"rpm\\\" under \"\n",
" \"--bdist-base; must be specified for RPM 2)\"),\n",
" ( 'dist-dir=', 'd',\n",
" \"directory to put final RPM files in \"\n",
" \"(and .spec files if --spec-only)\") , ('source-only', None, \"only generate source RPM\"),\n",
" ('binary-only', None,\n",
" \"only generate binary RPM\") ]\n",
" \n",
" def _format_changelog(self, changelog):\n",
" if not changelog:\n",
" return changelog\n",
" \n",
" \n",
" \n",
" \n",
" new_changelog = []\n",
" for line in string.split(string.strip(changelog), \n",
" '\\n'):\n",
" line = string.strip(line)\n",
" if line[0 ] =='*' :\n",
" new_changelog.extend ( ['' ])\n",
" elif line[0] == '-':\n",
" new_changelog.append(line )\n",
" else:\n",
" new_changelog.append(' '+ line)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Formatted:\n",
"\n",
"```python\n",
"class bdist_rpm:\n",
"\n",
" user_options = [\n",
" (\"bdist-base=\", None, \"base directory for creating built distributions\"),\n",
" (\n",
" \"rpm-base=\",\n",
" None,\n",
" 'base directory for creating RPMs (defaults to \"rpm\" under '\n",
" \"--bdist-base; must be specified for RPM 2)\",\n",
" ),\n",
" (\n",
" \"dist-dir=\",\n",
" \"d\",\n",
" \"directory to put final RPM files in \" \"(and .spec files if --spec-only)\",\n",
" ),\n",
" (\"source-only\", None, \"only generate source RPM\"),\n",
" (\"binary-only\", None, \"only generate binary RPM\"),\n",
" ]\n",
"\n",
" def _format_changelog(self, changelog):\n",
" if not changelog:\n",
" return changelog\n",
"\n",
" new_changelog = []\n",
" for line in string.split(string.strip(changelog), \"\\n\"):\n",
" line = string.strip(line)\n",
" if line[0] == \"*\":\n",
" new_changelog.extend([\"\"])\n",
" elif line[0] == \"-\":\n",
" new_changelog.append(line)\n",
" else:\n",
" new_changelog.append(\" \" + line)\n",
"```"
]
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment