Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2017 02:31
Show Gist options
  • Save anonymous/0d65e2bea8e9c8500f5090161254e718 to your computer and use it in GitHub Desktop.
Save anonymous/0d65e2bea8e9c8500f5090161254e718 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# 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[37]:
get_ipython().magic('reload_ext mdconvert')
# In[ ]:
# In[39]:
get_ipython().run_cell_magic('metadata', '', 'nbconvert:\n script: {}\nartifacts:\n [\n .py, \n ]')
# 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[41]:
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,
}
)
return resp
# self.log.warning("")
# In[42]:
resp = Post().postprocess(filename)
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment