{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "A sample notebook" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'hello'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "plt.plot(np.random.random(100))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A function for displaying the summary of a notebook object.\n", "It prints a simple summary, such as:\n", "\n", "```\n", " 1 markdown cells, total: 4 lines\n", " 5 code cells, total: 4 lines\n", " 1 heading cells, total: 1 lines\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def nb_info(nb):\n", " \"\"\"display a summary of the contents of a notebook\"\"\"\n", " cell_counts = {}\n", " cell_lines = {}\n", " \n", " for cell in nb.worksheets[0].cells:\n", " cell_type = cell.cell_type\n", " count = cell_counts.setdefault(cell_type, 0)\n", " lines = cell_counts.setdefault(cell_type, 0)\n", " cell_counts[cell_type] = count + 1\n", " try:\n", " content = cell.source\n", " except AttributeError:\n", " content = cell.input\n", " cell_lines[cell_type] = lines + len(content.splitlines())\n", " \n", " for cell_type in cell_counts:\n", " print \"%3i %10s cells, total: %3i lines\" % (cell_counts[cell_type], cell_type, cell_lines[cell_type])\n" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }