{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Get Contents\n", "\n", "Example notebook for grabbing headings from notebooks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Notebook File Directories\n", "\n", "Grab an ordered list of module notebook directories." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Part 01 Notebooks',\n", " 'Part 02 Notebooks',\n", " 'Part 03 Notebooks',\n", " 'Part 04 Notebooks',\n", " 'Part 05 Notebooks',\n", " 'Part 07 Notebooks',\n", " 'Part 08 Notebooks',\n", " 'Part 09 Notebooks',\n", " 'Part 10 Notebooks',\n", " 'Part 11 Notebooks',\n", " 'Part 12 Notebooks',\n", " 'Part 14 Notebooks',\n", " 'Part 15 Notebooks',\n", " 'Part 16 Notebooks',\n", " 'Part 20 Notebooks',\n", " 'Part 21 Notebooks',\n", " 'Part 22 Notebooks',\n", " 'Part 23 Notebooks',\n", " 'Part 25 Notebooks',\n", " 'Part 26 Notebooks']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "\n", "basedir='tm351'\n", "weeks = sorted([w for w in os.listdir(basedir) if w.startswith('Part') and w.endswith('Notebooks')])\n", "weeks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parse Notebook\n", "\n", "We can parse a notebook by grabbing markdown cells, splitting each line, and then looking for lines that start with a markdown heading marker (i.e. a `#`). Note that we will get false positives if a markdown cell contains styled code that contains a comment. (A full parser would have to guard out code marked up inside backticks, for example.)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "import nbformat as nb\n", "import IPython.nbformat.v4.nbbase as nb4\n" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "def getContentFromNotebook(notebook, md=None, debug=True):\n", " ''' Grab header lines from notebook markdown cells. '''\n", " md = [] if md is None else md\n", " tmpnb=nb.read(notebook,nb.NO_CONVERT)\n", " for i in tmpnb['cells']:\n", " if (i['cell_type']=='markdown'):\n", " for line in i['source'].splitlines():\n", " if line.startswith('#'):\n", " txt = line.replace('#','\\t')\n", " if debug: print(txt)\n", " md.append(txt)\n", " return md " ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "scrolled": false }, "outputs": [], "source": [ "def processNotebookFolders(weeks, debug=True):\n", " ''' Find notebooks in folders and extract headings from them. '''\n", " md=[]\n", " for week in weeks:\n", " notebooks = sorted([f for f in os.listdir('{}/{}'.format(basedir, week)) if f.endswith('.ipynb')])\n", " for notebook in notebooks:\n", " notebookpath = '{}/{}/{}'.format(basedir, week, notebook)\n", " txt = '{}/{}'.format( week, notebook)\n", " if debug: print(txt)\n", " md.append(txt)\n", " md = getContentFromNotebook(notebookpath, md, debug)\n", " if debug: print()\n", " return md\n", "\n", "md = processNotebookFolders(weeks, False)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Part 01 Notebooks/01.1 Getting started with IPython and Notebooks - Bootcamp.ipynb\r\n", "\t Python recap and IPython Notebook \"bootcamp\"\r\n", "\t\t Notebook cells\r\n", "\t\t\t Code cells\r\n", "\t\t\t Markdown cells and cell editting\r\n", "\t\t\t In passing, say 'Hi' to the command line...\r\n", "\t\t What next?\r\n", "Part 01 Notebooks/01.2 Python recap.ipynb\r\n", "\t Python recap\r\n", "\t\t Some Python basics\r\n" ] } ], "source": [ "def saveContentsAsMarkdown(md, fn='contents.md'):\n", " ''' Write out contents list to a markdown file. '''\n", " with open(fn, 'w') as outfile:\n", " outfile.write('\\n'.join(md))\n", " return fn\n", "\n", "fn = saveContentsAsMarkdown(md, fn='contents.md')\n", "#preview the out file\n", "!head $fn" ] }, { "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }