Data Processing¶
main.py¶
Data can be accessed by calling the quandl api using the following code:
quandl.ApiConfig.api_key = "GIVE YOUR API KEY HERE"
Once you have set the api key you can quandl datatables using .get method
data1 = quandl.get("LBMA/SILVER", collapse="monthly")
data2 = quandl.get("LBMA/GOLD", collapse="monthly")
Importing tab_commodity() function from commodity_select.py
from scripts.commodity_select import tab_commodity
We will be doing a bit a data processing to make sure data is consistent across commodities. Some data processing steps performed are
- Convert the output from quandl api get call into pandas dataframe
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
df1 = df1.fillna(0)
df2 = df2.fillna(0)
- Resetting the index on the dataframe
df1 = df1.reset_index()
df2 = df2.reset_index()
- Converting the Date column to datetime object
df1['Date'] = pd.to_datetime(df1['Date'])
df1['Date'] = pd.DatetimeIndex(df1['Date'])
df2['Date'] = pd.to_datetime(df2['Date'])
df2['Date'] = pd.DatetimeIndex(df2['Date'])
- Selecting a subset of dataframe
df1 = df1[['Date','USD']]
df2 = df2[['Date','USD (PM)']]
- Renaming the column in df2 and assigning new column to each dataframe
df2.rename(columns={'USD (PM)': 'USD'}, inplace=True)
df1['Commodity']='SILVER'
df2['Commodity']='GOLD'
frames = [df1,df2]
df_commodity = pd.concat(frames)
Also lets create a list of commodities that will be passed to the function in commodity_select.py
tab2 = tab_commodity(commodities,df_commodity)
Tab1 - Precious Commodities¶
commodity_select.py¶
Here is the commodity_select.py which is used to created Commodity Trend tab.
First step is to import the libraries required for passing data to the plot and dropdown select
import pandas as pd
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Select, Panel, HoverTool
from bokeh.plotting import figure
Now lets define the function tab_commodity that will be called from main.py
def tab_commodity(commodities, df_commodity):
p = figure(plot_width=960, plot_height=540, title="Commodity Trend",x_axis_type="datetime",sizing_mode="scale_width")
p.background_fill_color="#f5f5f5"
p.background_fill_alpha = 0.5
p.grid.grid_line_color="white"
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Value in USD'
p.axis.axis_line_color = None
hover = HoverTool()
select_commodity = Select(title='Commodity:',value=commodities[0],options=commodities)
x_val = df_commodity.loc[df_commodity['Commodity'] == select_commodity.value].Date.tolist()
y_val = df_commodity.loc[df_commodity['Commodity'] == select_commodity.value].USD.tolist()
source = ColumnDataSource(data=(dict(x=x_val,y=y_val)))
p.line('x','y',source=source,color='#ebbd5b',alpha=0.5,line_width=2)
hover.tooltips = [("Date", "@x{%F}"),("Value", "$@{y}")]
hover.formatters={
'x' : 'datetime'
}
hover.mode = 'vline'
p.tools.append(hover)
def callback(attr,old,new):
new_commodity = select_commodity.value
x_new = df_commodity.loc[df_commodity['Commodity'] == new_commodity].Date.tolist()
y_new = df_commodity.loc[df_commodity['Commodity'] == new_commodity].USD.tolist()
source_new = ColumnDataSource(data=(dict(x=x_new,y=y_new)))
source.data = source_new.data
select_commodity.on_change('value',callback)
layout = column(select_commodity,p)
tab2 = Panel(child = layout, title = 'Commodity Performance')
return tab2