#!/usr/bin/env python
# coding: utf-8
# # Make Jupyter even more magical
#
# ## (with cell magic extensions!)
#
# ### by Nicolas Kruchten, from Datacratic
#
#
# * Datacratic is Democratizing Machine Learning through the Machine Learning Database (http://mldb.ai/)
# * Based in Montreal, founded in 2010, venture-funded, 30 employees
# * Jupyter is shipped as part of MLDB as the main interface
#
# ## Intro to Jupyter
#
# First things first, you can run Python code in Jupyter.
# In[29]:
print "Hello, world!"
# Objects and functions stick around from cell to cell: I can define a function here...
# In[30]:
def do_stuff():
print "Hello again!"
# ...and call it there.
# In[31]:
do_stuff()
# Inspecting objects is easy too.
# In[4]:
import requests
get_ipython().run_line_magic('pinfo', 'requests')
# ## Markdown cells
#
# This is a Markdown cell.
#
# * this is a list item
# * and another
#
# ## Time for some Magic!
#
# Lines and cells that start with `%` are not interpreted like normal Python code: they are magical. Let's try a simple one first which will show us what magic is available.
# In[32]:
get_ipython().run_line_magic('lsmagic', '')
# In[33]:
get_ipython().run_line_magic('ls', '')
# In[34]:
get_ipython().run_line_magic('time', 'print "Do you have the time?"')
# In[35]:
get_ipython().run_cell_magic('timeit', '', 'x = 0\nfor i in xrange(10): x += i\n')
# In[36]:
get_ipython().run_cell_magic('bash', '', '\ncurl http://localhost:5000/get\n')
# ## First big idea of this talk: Jupyter cells are not just for Python!
#
# ## Third-party Magic
#
# Those were all built-in magics, but we can install and load third-party magic extensions too.
# In[37]:
get_ipython().run_line_magic('load_ext', 'sql')
# In[38]:
get_ipython().run_line_magic('sql', 'sqlite://')
# In[39]:
get_ipython().run_cell_magic('sql', '', '\nDROP TABLE IF EXISTS hockey;\nCREATE TABLE hockey ("Team", "Stanley Cups Won", "Country");\nINSERT INTO hockey VALUES ("Montreal Canadiens", 24, "Canada");\nINSERT INTO hockey VALUES ("Detroit Red Wings", 11, "USA");\nINSERT INTO hockey VALUES ("Boston Bruins", 6, "USA");\nINSERT INTO hockey VALUES ("Chicago Blackhawks", 11, "USA");\nINSERT INTO hockey VALUES ("Toronto Maple Leafs", 13, "Canada");\n')
# In[40]:
get_ipython().run_line_magic('sql', 'SELECT * FROM hockey ORDER BY "Stanley Cups Won" DESC LIMIT 3')
# Whoa, what happened there? That's not text, that's HTML!
#
# The answer is that when displaying an object, Jupyter tries to get an HTML representation of that object from a method called `_repr_html_` if it exists. Any object with such a method gets displayed this way.
# In[41]:
class Thing():
def _repr_html_(self):
return """