Python Forum

Full Version: create loop of subplot plotly dash without hardcode
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I make dash/plotly subplot, without repeating the subplot code ?

like :
stock_list = [ "MA","V","MSFT","AMZN","AAPL","TSLA"]
drawchart(stocklist)..loop
run dash....

I have made the hardcode version... spent for months , :-) hard enough for a non-IT guy.

The aim of this app, is to create list of candlestick chart for trader, e.g 40 stocks in one page , so that the trader can save a lot of time .



import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.graph_objects as go 
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output



# 1  create function for download  charts from yahoo

def ddown(code1,code2):
    import yfinance as yf
    data_df1 = yf.download(code1,start= "2020-01-01",end="2020-09-07")  # or use multiple download    
    data_df2 = yf.download(code2,start= "2020-01-01",end="2020-09-07")
   
    
    data_df1.reset_index(level=0, inplace=True)  # change index to column
    data_df2.reset_index(level=0, inplace=True)
   
    return data_df1,data_df2

#2 create function for generating candlestick subplot

def chart_draw(df1,df2):  
    
    fig = make_subplots(rows=2, cols=1)

    fig.add_trace(go.Candlestick(x = df1['Date'],
                                open = df1['Open'],
                                close = df1['Close'],
                                low = df1['Low'],
                                high = df1['High']),
                 row = 1, col = 1)
 
    fig.add_trace(go.Candlestick(x = df2['Date'],
                                open = df2['Open'],
                                close = df2['Close'],
                                low = df2['Low'],
                                high = df2['High']),
                 row = 2, col = 1)
       
    #fig.update_layout(width=900, height=900)
    fig.update_layout(xaxis_rangeslider_visible=False)
    fig.update_xaxes(rangeslider= {'visible':False}, row=2, col=1)
    fig.update_xaxes(rangeslider= {'visible':False}, row=3, col=1)
    
    return fig


#3    call download fucntions in 1 and subplot function in 2

var=ddown("MSFT","MA")    # call donwload stock quote function, store dresults in dataframe var

df1= var[0]         
df2= var[1]

   
fig1= chart_draw(df1,df2)       # call draw chart function in 2    

#4   generate dash site... 

app = dash.Dash()

app.layout = html.Div([
       dcc.Graph(figure=fig1)
])

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

some mistakes during copy ... Cry Cry Cry should remove duplicate import...
from datetime import datetime
import numpy as np
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go 
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output