Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2017 02:48
Show Gist options
  • Save anonymous/f88a1cef88fe7b50735606fdebc4dfb2 to your computer and use it in GitHub Desktop.
Save anonymous/f88a1cef88fe7b50735606fdebc4dfb2 to your computer and use it in GitHub Desktop.
This experiment shares a notebook to an anonymous gist.
Display the source blob
Display the rendered blob
Raw
{"nbformat":4,"nbformat_minor":2,"cells":[{"source":"# `POST` an anonymous [gist](https:\/\/developer.github.com\/v3\/gists\/)","cell_type":"markdown","metadata":{}},{"source":"filename = 'Untitled118.ipynb'","execution_count":1,"outputs":[],"cell_type":"code","metadata":{"collapsed":true}},{"source":"import ujson\nimport nbconvert\nimport nbformat\nimport os\nimport requests\nfrom toolz.curried import *","execution_count":36,"outputs":[],"cell_type":"code","metadata":{"collapsed":true}},{"source":"def _opener(input, filename=None):\n \n dirname, _filename = os.path.split(input)\n \n if filename and filename.startswith('.'):\n filename = _filename.replace('.ipynb', filename)\n \n filename = filename or _filename\n \n with open(filename) as f:\n ret = f.read()\n \n return filename, {\n 'content': ret\n }","execution_count":40,"outputs":[],"cell_type":"code","metadata":{"collapsed":true}},{"source":"class Post(nbconvert.postprocessors.PostProcessorBase):\n def postprocess(self, input):\n nb = nbformat.read(input, 4)\n md = nb['metadata']\n resp = requests.post(\n url=\"https:\/\/api.github.com\/gists\",\n json={\n 'files': merge(\n {\n filename: {\n \"content\": ujson.dumps(nb)\n }\n }, dict(\n _opener(input, filename=filename)\n for filename in 'artifacts' in md and md['artifacts'] or []\n )\n ),\n 'description': 'description' in md and md['description'] or \"\",\n 'public': True,\n }\n )\n self = get_ipython()\n self.log.warning(\"\"\"{}\"\"\".format(resp.json()['html_url']))\n return resp\n# self.log.warning(\"\")\nresp = Post().postprocess(filename)","execution_count":51,"outputs":[],"cell_type":"code","metadata":{"collapsed":true}},{"source":"# Autoformatting and linting.","cell_type":"markdown","metadata":{}},{"source":"try:\n output = juxt(\n local['yapf']['-i'], \n local['flake8']\n )('Untitled118.py')\nexcept Exception as e:\n print(e)","execution_count":92,"outputs":[{"text":"Command line: ['\/\/anaconda\/bin\/flake8', 'Untitled118.py']\nExit code: 1\nStdout: | Untitled118.py:13:1: E402 module level import not at top of file\n | Untitled118.py:14:1: E402 module level import not at top of file\n | Untitled118.py:15:1: E402 module level import not at top of file\n | Untitled118.py:16:1: E402 module level import not at top of file\n | Untitled118.py:17:1: E402 module level import not at top of file\n | Untitled118.py:18:1: E402 module level import not at top of file\n | Untitled118.py:18:1: F403 'from toolz.curried import *' used; unable to detect undefined names\n | Untitled118.py:23:1: F405 get_ipython may be undefined, or defined from star imports: toolz.curried\n | Untitled118.py:31:1: E303 too many blank lines (4)\n | Untitled118.py:33:1: F405 get_ipython may be undefined, or defined from star imports: toolz.curried\n | Untitled118.py:33:80: E501 line too long (115 > 79 characters)\n | Untitled118.py:39:1: W293 blank line contains whitespace\n | Untitled118.py:41:1: W293 blank line contains whitespace\n | Untitled118.py:44:1: W293 blank line contains whitespace\n | Untitled118.py:46:1: W293 blank line contains whitespace\n | Untitled118.py:49:1: W293 blank line contains whitespace\n | Untitled118.py:64:26: F405 merge may be undefined, or defined from star imports: toolz.curried\n | Untitled118.py:71:80: E501 line too long (83 > 79 characters)\n | Untitled118.py:78:16: F405 get_ipython may be undefined, or defined from star imports: toolz.curried\n | Untitled118.py:90:14: F405 juxt may be undefined, or defined from star imports: toolz.curried\n | Untitled118.py:91:9: F405 local may be undefined, or defined from star imports: toolz.curried\n | Untitled118.py:91:29: W291 trailing whitespace\n | Untitled118.py:92:9: F405 local may be undefined, or defined from star imports: toolz.curried\n | Untitled118.py:101:1: W391 blank line at end of file\n","output_type":"stream","name":"stdout"}],"cell_type":"code","metadata":{"collapsed":false}}],"metadata":{"artifacts":[".py"],"description":"This experiment shares a notebook to an anonymous gist.","language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","pygments_lexer":"ipython3","nbconvert_exporter":"python","name":"python","mimetype":"text\/x-python","version":"3.5.2"},"nbconvert":{"script":{}},"kernelspec":{"display_name":"root","name":"root","language":"python"},"anaconda-cloud":{}}}
# coding: utf-8
# # `POST` an anonymous [gist](https://developer.github.com/v3/gists/)
# In[1]:
filename = 'Untitled118.ipynb'
# In[36]:
import ujson
import nbconvert
import nbformat
import os
import requests
from toolz.curried import *
# In[40]:
def _opener(input, filename=None):
dirname, _filename = os.path.split(input)
if filename and filename.startswith('.'):
filename = _filename.replace('.ipynb', filename)
filename = filename or _filename
with open(filename) as f:
ret = f.read()
return filename, {
'content': ret
}
# In[51]:
class Post(nbconvert.postprocessors.PostProcessorBase):
def postprocess(self, input):
nb = nbformat.read(input, 4)
md = nb['metadata']
resp = requests.post(
url="https://api.github.com/gists",
json={
'files': merge(
{
filename: {
"content": ujson.dumps(nb)
}
}, dict(
_opener(input, filename=filename)
for filename in 'artifacts' in md and md['artifacts'] or []
)
),
'description': 'description' in md and md['description'] or "",
'public': True,
}
)
self = get_ipython()
self.log.warning("""{}""".format(resp.json()['html_url']))
return resp
# self.log.warning("")
resp = Post().postprocess(filename)
# # Autoformatting and linting.
# In[92]:
try:
output = juxt(
local['yapf']['-i'],
local['flake8']
)('Untitled118.py')
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment