# Best practice for loading libraries?
# Couldn't find what to do with 'magic' imports at the top

%load_ext autoreload
%autoreload 2
%matplotlib inline
%config InlineBackend.figure_format='retina' 

from __future__ import division

from itertools import combinations
import string

from IPython.display import IFrame, HTML, YouTubeVideo
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy as sp
import seaborn as sns; sns.set();

plt.rcParams['figure.figsize'] = (12, 8)
sns.set_style("darkgrid")
sns.set_context("poster", font_scale=1.3)

"""
Explanation: Best practices
Let's start with pep8 (https://www.python.org/dev/peps/pep-0008/)

Imports should be grouped in the following order:

standard library imports
related third party imports
local application/library specific imports

You should put a blank line between each group of imports.
Put any relevant all specification after the imports.
End of explanation
"""


YouTubeVideo("ZbrRrXiWBKc", width=800, height=600)

!pip install pivottablejs

df = pd.read_csv("../data/mps.csv")

df.head()

from pivottablejs import pivot_ui
pivot_ui(df)
# Province, Party, Average, Age, Heatmap

"""
Explanation: Pivot Tables w/ pandas
http://nicolas.kruchten.com/content/2015/09/jupyter_pivottablejs/
End of explanation
"""


# in select mode, shift j/k (to select multiple cells at once)
# split cell with ctrl shift -

first = 1

second = 2

third = 3

"""
Explanation: Keyboard shortcuts
End of explanation
"""


import rpy2

%load_ext rpy2.ipython

X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])

%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)

"""
Explanation: Floating Table of Contents
Creates a new button on the toolbar that pops up a table of contents that you can navigate by.
In your documentation if you indent by 4 spaces, you get monospaced code-style code so you can embed in a Markdown cell:
$ mkdir toc
$ cd toc

$ wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.js

$ wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.css
$ cd ..

$ jupyter-nbextension install --user toc

$ jupyter-nbextension enable toc/toc

You can also get syntax highlighting if you tell it the language that you're including: 
```bash
mkdir toc
cd toc
wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.js
wget https://raw.githubusercontent.com/minrk/ipython_extensions/master/nbextensions/toc.css
cd ..
jupyter-nbextension install --user toc
jupyter-nbextension enable toc/toc
```
R

pyRserve
rpy2
End of explanation
"""


