Python Forum
create loop of subplot plotly dash without hardcode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create loop of subplot plotly dash without hardcode
#1
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
 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot remove one of the x-ticks from a 2x1 subplot. generalzu10 1 188 Mar-20-2024, 04:24 AM
Last Post: generalzu10
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,523 Nov-07-2023, 09:49 AM
Last Post: buran
  How to hardcode event.state? (tkinter) philipbergwerf 1 1,544 Oct-24-2022, 01:55 PM
Last Post: deanhystad
  binning_endpoints ->plotly Luis_liverpool 0 808 Aug-09-2022, 10:13 AM
Last Post: Luis_liverpool
  Subplot - Plotting 4 plots on the same row Menthix 1 1,391 Nov-07-2021, 09:03 PM
Last Post: deanhystad
  plotly expression problem Visiting 2 1,963 May-16-2021, 12:28 AM
Last Post: Visiting
  Create Dynamic For Loop quest 3 4,290 Apr-26-2021, 02:03 PM
Last Post: ibreeden
  Python Matplotlib: How do I save (in pdf) all the graphs I create in a loop? JaneTan 1 8,779 Feb-28-2021, 06:20 PM
Last Post: Larz60+
  how to create pythonic codes including for loop and if statement? aupres 1 1,886 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  How to preserve x-axis labels despite deleted subplot? Mark17 1 1,888 Dec-23-2020, 09:02 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020