{ "metadata": { "name": "", "signature": "sha256:55ffc44f520f8332050c99c10b448057c7eb73610cbc6b9cc0ddb2676aeb4199" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import json\n", "from collections import defaultdict\n", "from datetime import datetime\n", "from IPython.display import HTML\n", "\n", "data = json.loads(open('data/commit_activity.json').read())\n", "series = defaultdict(int)\n", "for d in data:\n", " month = datetime.fromtimestamp(d['week']).month\n", " series[month] += d['total']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "HTML('''\n", "\n", "\n", "\n", "
\n", "\n", "''' % [{'name': 'flask', 'data': series.values()}])" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "
\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "import json\n", "from collections import defaultdict\n", "from datetime import datetime\n", "\n", "import jinja2\n", "from IPython.html.widgets import interact\n", "from IPython.html import widgets\n", "from IPython.display import display, display_pretty, Javascript, HTML\n", "from IPython.utils.traitlets import Any, Bool, Dict, List, Unicode\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "%%javascript\n", "require.config({\n", " paths: {\n", " chartjs: '//code.highcharts.com/highcharts',\n", " highcharts: '//code.highcharts.com/modules/exporting',\n", " sandsignika: '//www.highcharts.com/js/themes/sand-signika',\n", " },\n", " shim: {\n", " highcharts: {\n", " 'deps': ['chartjs', 'sandsignika']\n", " }\n", " }\n", "});" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "require.config({\n", " paths: {\n", " chartjs: '//code.highcharts.com/highcharts',\n", " highcharts: '//code.highcharts.com/modules/exporting',\n", " sandsignika: '//www.highcharts.com/js/themes/sand-signika',\n", " },\n", " shim: {\n", " highcharts: {\n", " 'deps': ['chartjs', 'sandsignika']\n", " }\n", " }\n", "});" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "def display_chart_highcharts(show_javascript, show, div):\n", " template = \"\"\"\n", " require([\"highcharts\"], function() {\n", " $('#chart_highcharts').highcharts({\n", " chart: {\n", " type: '{{ chart_type }}'\n", " },\n", " title: {\n", " text: '{{ title }}',\n", " x: -20 //center\n", " },\n", " subtitle: {\n", " text: 'https://github.com/mitsuhiko/flask',\n", " x: -20\n", " },\n", " xAxis: {\n", " categories: Highcharts.getOptions().lang.shortMonths\n", " },\n", " yAxis: {\n", " title: {\n", " text: '{{ yaxis }}'\n", " }\n", " },\n", " plotOptions: {{ plot }},\n", " series: {{ series }}\n", " });\n", " });\n", " \"\"\"\n", " \n", " with open('data/{}.json'.format(show.replace('by_', ''))) as f:\n", " data = json.loads(f.read())\n", " chart_type = 'line'\n", " plot = '{}'\n", " if show == 'by_code_frequency':\n", " pos_series = defaultdict(int)\n", " nag_series = defaultdict(int)\n", " for week, pos, nag in data:\n", " month = datetime.fromtimestamp(week).month\n", " pos_series[month] += pos\n", " nag_series[month] += -nag\n", " series = [{\n", " 'name': 'additions',\n", " 'data': pos_series.values()\n", " }, {\n", " 'name': 'deletions ',\n", " 'data': nag_series.values()\n", " }]\n", " title = 'Get the number of additions and deletions per week'\n", " yaxis = 'aggregate of the number'\n", " chart_type = 'column'\n", " plot = \"\"\"{\n", " column: {\n", " pointPadding: 0.2,\n", " borderWidth: 0\n", " }\n", " }\"\"\"\n", " elif show == 'by_contributors':\n", " data = sorted(data, key=lambda x: x['total'], reverse=True)[:5]\n", " series = []\n", " for i in data:\n", " name = i['author']['login']\n", " _series = defaultdict(int)\n", " for d in i['weeks']:\n", " month = datetime.fromtimestamp(d['w']).month\n", " _series[month] += (d['a'] + d['c'] + d['d'])\n", " series.append({\n", " 'name': name.encode('utf-8'),\n", " 'data': _series.values()\n", " })\n", " title = 'Get contributors list with additions, deletions, and commit counts'\n", " yaxis = 'commit number'\n", " \n", " rendered_template = jinja2.Template(template).render(\n", " title=title, yaxis=yaxis, series=series,\n", " chart_type=chart_type, plot=plot\n", " )\n", " \n", " if show_javascript:\n", " display(widgets.PopupWidget(children=(widgets.HTMLWidget(\n", " value='
{}
'.format(rendered_template)),)))\n", " display(Javascript(rendered_template))\n", "\n", "i = interact(\n", " display_chart_highcharts,\n", " show_javascript=widgets.CheckboxWidget(value=False),\n", " show=widgets.DropdownWidget(\n", " values={'Code frequency':'by_code_frequency', 'Contributors': 'by_contributors'},\n", " value='by_contributors'\n", " ),\n", " div=widgets.HTMLWidget(value='
')\n", ")\n" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "\n", " require([\"highcharts\"], function() {\n", " $('#chart_highcharts').highcharts({\n", " chart: {\n", " type: 'line'\n", " },\n", " title: {\n", " text: 'Get contributors list with additions, deletions, and commit counts',\n", " x: -20 //center\n", " },\n", " subtitle: {\n", " text: 'https://github.com/mitsuhiko/flask',\n", " x: -20\n", " },\n", " xAxis: {\n", " categories: Highcharts.getOptions().lang.shortMonths\n", " },\n", " yAxis: {\n", " title: {\n", " text: 'commit number'\n", " }\n", " },\n", " plotOptions: {},\n", " series: [{'data': [369, 276, 598, 15018, 8358, 5181, 6312, 10574, 1718, 1548, 188, 229], 'name': 'mitsuhiko'}, {'data': [957, 15, 134, 1022, 60, 740, 32, 24, 5, 103, 0, 0], 'name': 'rduplain'}, {'data': [0, 0, 25, 0, 258, 0, 18, 30089, 32, 13, 6, 8], 'name': 'untitaker'}, {'data': [0, 12, 9, 78, 700, 38, 63, 29, 5, 44, 0, 91], 'name': 'DasIch'}, {'data': [0, 0, 0, 0, 0, 0, 0, 0, 35, 83, 866, 0], 'name': 'defuz'}]\n", " });\n", " });\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 } ], "metadata": {} } ] }