How to show an error message when the interval of historical data is too small or big?

Hello everybody,

I am developing the following app for showing historical data with the yfinance module:

image

For this I am using the following function:

def visualization_history(ticker, start_date, end_date, chosen_interval):

tickerSymbol = str(ticker)
            
tickerData = yf.Ticker(tickerSymbol)
                   
                
#DATES FOR CLOSE AND VOLUME    
            
tickerDf = tickerData.history(interval=chosen_interval, start=start_date, end=end_date)
                

#CLOSING PRICE
    
st.write("""# Stock closing price""")
    
st.line_chart(data = tickerDf.Close)
    
    
    
#VOLUME
    
st.write("""# Stock volume""")
    
st.line_chart(data = tickerDf.Volume)


return()

For certain values I get the following error message in the console: - BTC-USD: 1m data not available for startTime=1410908400 and endTime=1622930400. Only 7 days worth of 1m granularity data are allowed to be fetched per request.

I would like to display an error when this happen. For example “The graph cannot be shown for this interval” or something similar.

I was trying something like:

try:
visualization_history(interval=chosen_interval, start=start_date, end=end_date)
except ValueError:
st.error(‘Please enter a valid interval’)

without any luck. Can anybody help?

Thanks a lot