Plotly example

The following sets up a python virtual environment, installs some modules and uses plotly with yfanance to create a website with a graph.

sudo apt install python3-venv
python -m venv test
cd test/bin/  
source activate  

#note to leave venv type in 'deactivate'

pip install yfinance pandas plotly


cat > test2.py <<"EOF"
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
from datetime import datetime, timedelta

# Define the start and end dates for the last 12 months
end_date = datetime.now()
start_date = end_date - timedelta(days=365)

# Download historical data for Amazon (AMZN)
data = yf.download('AMZN', start=start_date, end=end_date)

# Create a candle stick chart using plotly
fig = go.Figure(data=[go.Candlestick(x=data.index,
                                     open=data['Open'],
                                     high=data['High'],
                                     low=data['Low'],
                                     close=data['Close'])])

# Customize the chart layout
fig.update_layout(title='Amazon (AMZN) Stock Chart - Last 12 Months',
                  xaxis_title='Date',
                  yaxis_title='Price ($)',
                  xaxis_rangeslider_visible=False)

# Save the chart as an HTML file
# fig.write_html('amazon_stock_chart.html')

# Save the chart as an HTML file with separate plotly.js file
fig.write_html('amazon_stock_chart.html', include_plotlyjs='directory', full_html=True)
EOF