daily log 12.07.20

less than 1 minute read

UPYD

THINGS I LEARNED TODAY:

  1. the order of the params of the @callback function need to match the receivers in the “update_graph” function

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas_datareader.data as web # requires v0.6.0 or later
from datetime import datetime
import pandas as pd

from dotenv import load_dotenv
load_dotenv()
import os
TIINGO_API_KEY = os.getenv('TIINGO_API_KEY')

app = dash.Dash()

app.layout = html.Div([
  html.H1('Stock Ticker Dashboard'),
  html.Div([
      html.H3('Enter a stock symbol:'),
      dcc.Input(id='my_ticker_symbol',value='TSLA'),
  ]),
  html.Div([html.H3('Select a start and end date:'), 
    dcc.DatePickerRange(id='my_date_picker',
    min_date_allowed=datetime(2015,1,1), 
    max_date_allowed=datetime.today(), 
    start_date=datetime(2018, 1,1), 
    end_date=datetime.today())
  ], style={'display': 'inline-block'}),
  html.Div([
    html.Button(id='submit-button', 
    n_clicks=0, 
    children='Submit', 
    style={'fontSize': 24, 'marginLeft': '30px'})
  ]),
  dcc.Graph(id='my_graph', figure={'data': [{'x':[1,2], 'y': [3.4]}]})
])

@app.callback(
  Output('my_graph', 'figure'),
  [Input('submit-button', 'n_clicks')],
  [State('my_ticker_symbol', 'value'),
    State('my_date_picker', 'start_date'),
    State('my_date_picker', 'end_date'),
  ])

def update_graph(n_clicks, stock_ticker, start_date, end_date):
  start = datetime.strptime(start_date[:10], '%Y-%m-%d')
  end = datetime.strptime(end_date[:10], '%Y-%m-%d')
  df = web.get_data_tiingo(stock_ticker, start, end, api_key=TIINGO_API_KEY)
  df.index = df.index.get_level_values('date') 
  fig = {'data':[{'x':df.index, 'y':df['adjClose']}], 'layout':{'title':stock_ticker}}

  return fig


if __name__ == '__main__':
  app.run_server()