Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Last active April 6, 2016 18:47
Show Gist options
  • Save tonyfast/62e0d05a85c06f1938d1f8c17cef6dee to your computer and use it in GitHub Desktop.
Save tonyfast/62e0d05a85c06f1938d1f8c17cef6dee to your computer and use it in GitHub Desktop.
Write Literate Python in Markdown Magic Cells
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"\"\"These code below creates the markdown magics class.\"\"\"\n",
"import mistune, IPython, jinja2\n",
"\n",
"class LiterateRenderer(mistune.Renderer):\n",
" \"\"\"Executes code for on Markdown code fences for the language ``python``\"\"\"\n",
" def __init__( self, ip, *args, **kwargs):\n",
" self.ip = ip\n",
" super().__init__(*args, **kwargs)\n",
"\n",
" def block_code( self, text, lang ):\n",
" if lang in ['python']:\n",
" self.ip.run_code(text)\n",
" return super().block_code( text, lang )\n",
"\n",
"class MarkdownerTemplate( IPython.core.display.Markdown ):\n",
" import yaml\n",
" def __init__( self, env = jinja2.Environment(), ip=IPython.get_ipython(), frontmatter=\"\"\"\"\"\", *args, **kwargs):\n",
" self.env = env\n",
" self.ip = ip\n",
" self.frontmatter = frontmatter\n",
" super().__init__(*args, **kwargs)\n",
" if self.frontmatter:\n",
" self.frontmatter = self.yaml.load(self.env.from_string( self.frontmatter ).render( self.ip.user_ns ))\n",
" else:\n",
" self.frontmatter = {}\n",
" self.template = self.env.from_string( self.data )\n",
"\n",
" def __add__( self, payload ):\n",
" if payload in ['*']:\n",
" payload = self.ip.user_ns\n",
" return self.template.render( **payload )\n",
" def __mul__( self, payload ):\n",
" return '\\n'.join([self.template.render(**load) for load in payload ])\n",
" \n",
" def _repr_markdown_( self ):\n",
" mistune.markdown( self.data, renderer = LiterateRenderer(ip=self.ip,escape=False) )\n",
" return self + {**self.frontmatter, **self.ip.user_ns}\n",
"\n",
"@IPython.core.magic.magics_class\n",
"class MarkdownMagic(IPython.core.magic.Magics):\n",
" import jinja2, yaml\n",
" ip = get_ipython() \n",
" def __init__( self ):\n",
" self.env = self.jinja2.Environment(loader=jinja2.DictLoader({}))\n",
" super().__init__()\n",
" self.ip.register_magics(self)\n",
" \n",
" @IPython.core.magic.cell_magic\n",
" @IPython.core.magic_arguments.magic_arguments()\n",
" @IPython.core.magic_arguments.argument( \"name\", default=\"\"\"\"\"\", nargs=\"?\", help=\"\"\"Name of local variable to set to parsed value\"\"\")\n",
" def markdown(self, line, cell):\n",
" line = line.strip()\n",
" args = IPython.core.magic_arguments.parse_argstring(self.markdown, line)\n",
" if cell.startswith('---'):\n",
" frontmatter, cell = cell.lstrip('---').split('---',1)\n",
" else: \n",
" frontmatter = {}\n",
" display = MarkdownerTemplate( self.env, self.ip, frontmatter, data=cell ) # Literate python execution\n",
" if args.name:\n",
" self.ip.user_ns[args.name] = display\n",
" return display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import ipywidgets\n",
"md = MarkdownMagic(); # Initialize markdown magic\n",
"%reload_ext yamlmagic\n",
"%reload_ext jademagic"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" require(\n",
" [\n",
" \"notebook/js/codecell\",\n",
" \"codemirror/mode/jade/jade\"\n",
" ],\n",
" function(cc){\n",
" cc.CodeCell.options_default.highlight_modes.magic_jade = {\n",
" reg: [\"^%%jade\"]\n",
" }\n",
" }\n",
" );\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"<div id=\"toggle-button\" style=\"position: fixed;right: 10px; bottom: 10px; \" class=\"btn btn-default\">Toggle inputs </div>\n",
"<script>$('#toggle-button').click( function(){$('div.input').toggle()} )</script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%jade \n",
".btn.btn-default#toggle-button(style=\"position: fixed;right: 10px; bottom: 10px; \") Toggle inputs \n",
"script $('#toggle-button').click( function(){$('div.input').toggle()} )"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"# Viewing some data in a webpage\n",
"\n",
"Let's download some data to discuss. In this example we will look at a user's\n",
"gists. We will use a textarea widget to define the user name. The widget is\n",
"dynamic and can be used to build in interactive dashboard.\n",
"\n",
"---\n",
"```python\n",
"import ipywidgets\n",
"username = ipywidgets.Text('tonyfast', description='username')\n",
"```\n",
"---\n",
"\n",
"> The sample data for this demo will use github data.\n",
"\n",
"---\n",
"```python\n",
"import requests\n",
"url = \"https://api.github.com/users/{username}/repos\".format(username=username.value)\n",
"response = requests.get( url=url )\n",
"```\n",
"---\n",
"\n",
"The response returns **30** entries.\n",
"\n",
"## Response information\n",
"\n",
"Load the response information as a dataframe.\n",
"\n",
"---\n",
"```python\n",
"import pandas\n",
"df = pandas.DataFrame( response.json())\n",
"```\n",
"---\n",
"\n",
"### A sample of the response below\n",
"\n",
"<table border=\"1\" class=\"dataframe table table-bordered table-hover table-striped\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>archive_url</th>\n",
" <th>assignees_url</th>\n",
" <th>blobs_url</th>\n",
" <th>branches_url</th>\n",
" <th>clone_url</th>\n",
" <th>collaborators_url</th>\n",
" <th>comments_url</th>\n",
" <th>commits_url</th>\n",
" <th>compare_url</th>\n",
" <th>contents_url</th>\n",
" <th>contributors_url</th>\n",
" <th>created_at</th>\n",
" <th>default_branch</th>\n",
" <th>deployments_url</th>\n",
" <th>description</th>\n",
" <th>downloads_url</th>\n",
" <th>events_url</th>\n",
" <th>fork</th>\n",
" <th>forks</th>\n",
" <th>forks_count</th>\n",
" <th>forks_url</th>\n",
" <th>full_name</th>\n",
" <th>git_commits_url</th>\n",
" <th>git_refs_url</th>\n",
" <th>git_tags_url</th>\n",
" <th>git_url</th>\n",
" <th>has_downloads</th>\n",
" <th>has_issues</th>\n",
" <th>has_pages</th>\n",
" <th>has_wiki</th>\n",
" <th>homepage</th>\n",
" <th>hooks_url</th>\n",
" <th>html_url</th>\n",
" <th>id</th>\n",
" <th>issue_comment_url</th>\n",
" <th>issue_events_url</th>\n",
" <th>issues_url</th>\n",
" <th>keys_url</th>\n",
" <th>labels_url</th>\n",
" <th>language</th>\n",
" <th>languages_url</th>\n",
" <th>merges_url</th>\n",
" <th>milestones_url</th>\n",
" <th>mirror_url</th>\n",
" <th>name</th>\n",
" <th>notifications_url</th>\n",
" <th>open_issues</th>\n",
" <th>open_issues_count</th>\n",
" <th>owner</th>\n",
" <th>private</th>\n",
" <th>pulls_url</th>\n",
" <th>pushed_at</th>\n",
" <th>releases_url</th>\n",
" <th>size</th>\n",
" <th>ssh_url</th>\n",
" <th>stargazers_count</th>\n",
" <th>stargazers_url</th>\n",
" <th>statuses_url</th>\n",
" <th>subscribers_url</th>\n",
" <th>subscription_url</th>\n",
" <th>svn_url</th>\n",
" <th>tags_url</th>\n",
" <th>teams_url</th>\n",
" <th>trees_url</th>\n",
" <th>updated_at</th>\n",
" <th>url</th>\n",
" <th>watchers</th>\n",
" <th>watchers_count</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://github.com/tonyfast/Aluminum-Surface-R...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>2014-05-17T15:46:15Z</td>\n",
" <td>gh-pages</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>Analytics and Datasets for Mark Stoudt's Alumi...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>False</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>tonyfast/Aluminum-Surface-Roughness</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>git://github.com/tonyfast/Aluminum-Surface-Rou...</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>http://tonyfast.com/Aluminum-Surface-Roughness</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://github.com/tonyfast/Aluminum-Surface-R...</td>\n",
" <td>19890085</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>Matlab</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>None</td>\n",
" <td>Aluminum-Surface-Roughness</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>{'organizations_url': 'https://api.github.com/...</td>\n",
" <td>False</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>2014-06-21T13:54:43Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>146304</td>\n",
" <td>git@github.com:tonyfast/Aluminum-Surface-Rough...</td>\n",
" <td>0</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://github.com/tonyfast/Aluminum-Surface-R...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>2014-11-21T21:36:08Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/Aluminum...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/{ar...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/ass...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/git...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/bra...</td>\n",
" <td>https://github.com/tonyfast/ASCL.git</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/col...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/com...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/com...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/com...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/con...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/con...</td>\n",
" <td>2015-02-18T18:49:12Z</td>\n",
" <td>gh-pages</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/dep...</td>\n",
" <td>ASCL public data in json format. Probably.</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/dow...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/events</td>\n",
" <td>True</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/forks</td>\n",
" <td>tonyfast/ASCL</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/git...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/git...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/git...</td>\n",
" <td>git://github.com/tonyfast/ASCL.git</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>http://tonyfast.com/ASCL</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/hooks</td>\n",
" <td>https://github.com/tonyfast/ASCL</td>\n",
" <td>30980651</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/iss...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/iss...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/iss...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/key...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/lab...</td>\n",
" <td>HTML</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/lan...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/merges</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/mil...</td>\n",
" <td>None</td>\n",
" <td>ASCL</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/not...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>{'organizations_url': 'https://api.github.com/...</td>\n",
" <td>False</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/pul...</td>\n",
" <td>2015-02-19T17:49:56Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/rel...</td>\n",
" <td>782</td>\n",
" <td>git@github.com:tonyfast/ASCL.git</td>\n",
" <td>0</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/sta...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/sta...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/sub...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/sub...</td>\n",
" <td>https://github.com/tonyfast/ASCL</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/tags</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/teams</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL/git...</td>\n",
" <td>2015-02-19T17:49:56Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/ASCL</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://github.com/tonyfast/ATLatlas.me.git</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>2015-02-27T21:40:24Z</td>\n",
" <td>gh-pages</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>Repo for Goodie Hack on 2/27-2/28</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>False</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>tonyfast/ATLatlas.me</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>git://github.com/tonyfast/ATLatlas.me.git</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>None</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://github.com/tonyfast/ATLatlas.me</td>\n",
" <td>31440370</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>HTML</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>None</td>\n",
" <td>ATLatlas.me</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>{'organizations_url': 'https://api.github.com/...</td>\n",
" <td>False</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>2015-02-28T21:21:29Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>740</td>\n",
" <td>git@github.com:tonyfast/ATLatlas.me.git</td>\n",
" <td>0</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://github.com/tonyfast/ATLatlas.me</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas...</td>\n",
" <td>2015-02-28T21:21:29Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/ATLatlas.me</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://github.com/tonyfast/Atomic-Positions.git</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>2014-05-13T21:00:38Z</td>\n",
" <td>master</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>Atomic Positions from LAMMPS molecular dynamic...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>False</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>tonyfast/Atomic-Positions</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>git://github.com/tonyfast/Atomic-Positions.git</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" <td>tonyfast.com/Atomic-Positions</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://github.com/tonyfast/Atomic-Positions</td>\n",
" <td>19755742</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>None</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>None</td>\n",
" <td>Atomic-Positions</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>{'organizations_url': 'https://api.github.com/...</td>\n",
" <td>False</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>2014-05-15T16:08:11Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>18784</td>\n",
" <td>git@github.com:tonyfast/Atomic-Positions.git</td>\n",
" <td>0</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://github.com/tonyfast/Atomic-Positions</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>2014-07-30T15:38:40Z</td>\n",
" <td>https://api.github.com/repos/tonyfast/Atomic-P...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"\n",
"\n",
"### Table Columns\n",
"\n",
"The table contains the following columns: *archive_url*, *assignees_url*, *blobs_url*, *branches_url*, *clone_url*, *collaborators_url*, *comments_url*, *commits_url*, *compare_url*, *contents_url*, *contributors_url*, *created_at*, *default_branch*, *deployments_url*, *description*, *downloads_url*, *events_url*, *fork*, *forks*, *forks_count*, *forks_url*, *full_name*, *git_commits_url*, *git_refs_url*, *git_tags_url*, *git_url*, *has_downloads*, *has_issues*, *has_pages*, *has_wiki*, *homepage*, *hooks_url*, *html_url*, *id*, *issue_comment_url*, *issue_events_url*, *issues_url*, *keys_url*, *labels_url*, *language*, *languages_url*, *merges_url*, *milestones_url*, *mirror_url*, *name*, *notifications_url*, *open_issues*, *open_issues_count*, *owner*, *private*, *pulls_url*, *pushed_at*, *releases_url*, *size*, *ssh_url*, *stargazers_count*, *stargazers_url*, *statuses_url*, *subscribers_url*, *subscription_url*, *svn_url*, *tags_url*, *teams_url*, *trees_url*, *updated_at*, *url*, *watchers*, *watchers_count*, "
],
"text/plain": [
"<__main__.MarkdownerTemplate object>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%markdown\n",
"\n",
"# Viewing some data in a webpage\n",
"\n",
"Let's download some data to discuss. In this example we will look at a user's\n",
"gists. We will use a textarea widget to define the user name. The widget is\n",
"dynamic and can be used to build in interactive dashboard.\n",
"\n",
"---\n",
"```python\n",
"import ipywidgets\n",
"username = ipywidgets.Text('tonyfast', description='username')\n",
"```\n",
"---\n",
"\n",
"> The sample data for this demo will use github data.\n",
"\n",
"---\n",
"```python\n",
"import requests\n",
"url = \"https://api.github.com/users/{username}/repos\".format(username=username.value)\n",
"response = requests.get( url=url )\n",
"```\n",
"---\n",
"\n",
"The response returns **{{__builtin__.len(response.json())}}** entries.\n",
"\n",
"## Response information\n",
"\n",
"Load the response information as a dataframe.\n",
"\n",
"---\n",
"```python\n",
"import pandas\n",
"df = pandas.DataFrame( response.json())\n",
"```\n",
"---\n",
"\n",
"### A sample of the response below\n",
"\n",
"{{df.iloc[:4].to_html(classes='table table-bordered table-hover table-striped')}}\n",
"\n",
"\n",
"### Table Columns\n",
"\n",
"The table contains the following columns: {% for c in df.columns %}*{{c}}*, {% endfor %}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"\n",
"## This Markdown Cell contains test data\n",
"\n",
"Test data can be loaded in from the global namespace to test the jinja2 template. \n",
"The bare template can used later for other data sources.\n",
"\n",
"\n",
"> What is the meaning of life?\n",
"\n",
"> 42"
],
"text/plain": [
"<__main__.MarkdownerTemplate object>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%markdown panel \n",
"---\n",
"test: What is the meaning of life?\n",
"answer: 42\n",
"---\n",
"\n",
"## This Markdown Cell contains test data\n",
"\n",
"Test data can be loaded in from the global namespace to test the jinja2 template. \n",
"The bare template can used later for other data sources.\n",
"\n",
"\n",
"> {{test}}\n",
"\n",
"> {{answer}}\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"<div class=\"panel panel-default col-md-4\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">tonyfast/Brazed-Wires</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" Analysis of Volumetric Information on Brazed Wires with NRL.\n",
" </div>\n",
"</div>"
],
"text/plain": [
"<__main__.MarkdownerTemplate object>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%markdown panel\n",
"---\n",
"{{response.json()[10]}}\n",
"---\n",
"<div class=\"panel panel-default col-md-4\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">{{full_name}}</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" {{description}}\n",
" </div>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"<div class=\"panel panel-default col-md-4\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">tonyfast/d3.template</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" Web templates from structured data\n",
" </div>\n",
"</div>\n",
"\n",
"<div class=\"panel panel-default col-md-4\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">tonyfast/djatashape</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" \n",
" </div>\n",
"</div>\n",
"\n",
"<div class=\"panel panel-default col-md-4\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">tonyfast/d3-dom-tutorial</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" Use d3, handlebars, and bootstrap to populate a web page\n",
" </div>\n",
"</div>\n",
"\n",
"<div class=\"panel panel-default col-md-4\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">tonyfast/Aluminum-Surface-Roughness</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" Analytics and Datasets for Mark Stoudt's Aluminum Surface Roughness Datasets at NIST\n",
" </div>\n",
"</div>\n",
"\n",
"<div class=\"panel panel-default col-md-4\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">tonyfast/ASCL</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" ASCL public data in json format. Probably.\n",
" </div>\n",
"</div>"
],
"text/plain": [
"<__main__.MarkdownerTemplate object>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%markdown panels\n",
"{{panel * df.sort_values('stargazers_count',ascending=False).iloc[:5].to_dict(orient='records')}}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment