Reference: https://blog.dominodatalab.com/interactive-dashboards-in-jupyter/
%pylab inline
#%load_ext RWinOut
Populating the interactive namespace from numpy and matplotlib
Documentation is provided here: https://ipywidgets.readthedocs.io/en/stable/
from ipywidgets import widgets
from IPython.display import display
text = widgets.Text()
display(text)
def print_input(x):
print(x.value)
text.on_submit(print_input)
Hello Hello again!!
button = widgets.Button(description='Click here')
display(button)
j = 100
def show_click(x):
global j
j = j + 1
print("Clicked",j)
button.on_click(show_click)
Clicked 101 Clicked 102 Clicked 103
from ipywidgets import interact, interactive, fixed, interact_manual
def f(x):
return x
interact(f,x=100)
print("thanks")
thanks
interact(f,x=True)
<function __main__.f(x)>
interact(f,x='text')
<function __main__.f(x)>
def f2(x):
print("Final = ",x**2)
interact(f2,x=10)
<function __main__.f2(x)>
def f3(x):
print("Final = %.2f" % x**2)
interact(f3,x=(2,5,0.1))
<function __main__.f3(x)>
def pltsin(x,y):
t = arange(x)
plot(sin(t*y))
interact(pltsin,x=(2,50),y=(1,20))
<function __main__.pltsin(x, y)>
from scipy.stats import norm
from ipywidgets import interact, interactive, fixed, interact_manual, IntSlider, Output
def BS(s,k,t,v,rf):
d1 = (log(s/k)+(rf+0.5*v**2)*t)/(v*sqrt(t))
d2 = d1 - v*sqrt(t)
xc = s*norm.cdf(d1) - k*exp(-rf*t)*norm.cdf(d2)
xp = -s*norm.cdf(-d1) + k*exp(-rf*t)*norm.cdf(-d2)
print('Call =',xc,' | Put =',xp)
interact(BS, s=(10.0,50.0,0.01), k=(10.0,50.0,0.01), t=(0.0,10.0,0.01), v=(0.01,0.50,0.01), rf=(0.0,0.10,0.01))
print("----")
----
First see this in R, then try it in Python
%load_ext rpy2.ipython
%%R
library(quantmod)
getSymbols("GOOG")
GOOG = as.data.frame(GOOG)
print(class(GOOG))
print(head(GOOG))
/Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Loading required package: xts warnings.warn(x, RRuntimeWarning) /Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Loading required package: zoo warnings.warn(x, RRuntimeWarning) /Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Attaching package: ‘zoo’ warnings.warn(x, RRuntimeWarning) /Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: The following objects are masked from ‘package:base’: as.Date, as.Date.numeric warnings.warn(x, RRuntimeWarning) /Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Loading required package: TTR warnings.warn(x, RRuntimeWarning) /Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Version 0.4-0 included new data defaults. See ?getSymbols. Learn from a quantmod author: https://www.datacamp.com/courses/importing-and-managing-financial-data-in-r warnings.warn(x, RRuntimeWarning) /Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: ‘getSymbols’ currently uses auto.assign=TRUE by default, but will use auto.assign=FALSE in 0.5-0. You will still be able to use ‘loadSymbols’ to automatically load data. getOption("getSymbols.env") and getOption("getSymbols.auto.assign") will still be checked for alternate defaults. This message is shown once per session and may be disabled by setting options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details. warnings.warn(x, RRuntimeWarning) /Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: WARNING: There have been significant changes to Yahoo Finance data. Please see the Warning section of ‘?getSymbols.yahoo’ for details. This message is shown once per session and may be disabled by setting options("getSymbols.yahoo.warning"=FALSE). warnings.warn(x, RRuntimeWarning)
[1] "data.frame" GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted 2007-01-03 231.4944 236.7899 229.0652 232.2842 15513200 232.2842 2007-01-04 232.9847 240.4114 232.6618 240.0686 15877700 240.0686 2007-01-05 239.6910 242.1749 237.5102 242.0209 13833500 242.0209 2007-01-08 242.2693 243.3522 239.5420 240.2276 9570600 240.2276 2007-01-09 241.1565 242.5475 239.0452 241.1814 10832700 241.1814 2007-01-10 240.6498 245.1803 239.4625 243.1486 12014600 243.1486
%%R
library(DT)
datatable(GOOG,filter="top")
import pandas as pd
goog = %Rget GOOG
goog = pd.DataFrame(goog)
goog.head()
/Users/srdas/anaconda3/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
GOOG.Open | GOOG.High | GOOG.Low | GOOG.Close | GOOG.Volume | GOOG.Adjusted | |
---|---|---|---|---|---|---|
0 | 231.494354 | 236.789917 | 229.065155 | 232.284210 | 15513200.0 | 232.284210 |
1 | 232.984665 | 240.411362 | 232.661758 | 240.068588 | 15877700.0 | 240.068588 |
2 | 239.691040 | 242.174881 | 237.510223 | 242.020889 | 13833500.0 | 242.020889 |
3 | 242.269272 | 243.352234 | 239.542007 | 240.227554 | 9570600.0 | 240.227554 |
4 | 241.156509 | 242.547470 | 239.045242 | 241.181351 | 10832700.0 | 241.181351 |
# To save the data frame as html
goog.to_html(open('my_file.html', 'w'))
html = goog.to_html().replace('<table','<table class="tableBoot" id="myTable"')
f = open('my_file.html', 'w')
f.write(html)
f.close()
import qgrid
qgrid.show_grid(goog, show_toolbar=True)