from flask import Flask, render_template, request, jsonify from flask_socketio import SocketIO import yfinance as yf from datetime import datetime, timedelta import plotly.graph_objs as go app = Flask(__name__) socketio = SocketIO(app) @app.route("/") def index(): return render_template("test.html") @socketio.on('get_graph') def get_graph(ticker): # Define the stock ticker symbol and the date range ticker_symbol = ticker end_date = datetime.today() start_date = end_date - timedelta(days=90) # Fetch historical data using yfinance data = yf.download(ticker_symbol, start=start_date, end=end_date) # Create a candlestick graph using Plotly fig = go.Figure(data=[go.Candlestick(x=data.index.strftime('%Y-%m-%d').tolist(), open=data['Open'].tolist(), high=data['High'].tolist(), low=data['Low'].tolist(), close=data['Close'].tolist())]) # Customize the layout fig.update_layout(title=f'Candlestick Chart for {ticker_symbol} in the Last 90 Days', xaxis_title='Date', yaxis_title='Price', xaxis_rangeslider_visible=False) # # convert the fig to HTML DIV element # graph_html = fig.to_html(full_html=False,include_plotlyjs = False) # print(len(graph_html)) # # socketio.emit('graph', fig.to_html(full_html=False,include_plotlyjs = False)) graph_data = fig.to_plotly_json() socketio.emit('graph',graph_data) print("Graph is finished") if __name__ == "__main__": socketio.run(app, host='0.0.0.0', port=7860)