基于标准Python脚本实现弹出式蜡烛图:Jupyter代码改造方案咨询
Turn Your Jupyter Candlestick Chart into a Pop-Up Tool
Got it, let's fix this up. Your original code works great in Jupyter, but to get a pop-up version, we need to adjust how Plotly renders the chart, plus fix a small issue with the outdated data source (Morningstar no longer works with pandas_datareader). Here are two straightforward solutions:
Solution 1: Pop-Up Browser Window (Easiest, Full Interactivity)
This keeps all of Plotly's awesome interactive features (zoom, hover data, etc.) and just opens the chart in a separate browser window instead of embedding it in Jupyter.
Modified Code
import plotly.graph_objs as go import plotly.offline as py_offline import yfinance as yf # Replaces broken pandas_datareader Morningstar from datetime import datetime # Grab AAPL stock data (2020-2024 as an example) start = datetime(2020, 1, 1) end = datetime(2024, 1, 1) df = yf.download("AAPL", start=start, end=end) # Build the candlestick trace candlestick = go.Candlestick( x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], name='AAPL Candlesticks' ) # Add some clean layout tweaks (optional but makes it look better) layout = go.Layout( title='AAPL Stock Candlestick Chart', xaxis_title='Date', yaxis_title='Price (USD)', hovermode='x unified' # Shows all data points for a given date on hover ) fig = go.Figure(data=[candlestick], layout=layout) # This is the key change: opens the chart in a new browser window py_offline.show(fig, filename='aapl_candlestick_popup.html')
Key Changes:
- Replaced the broken data source: Morningstar stopped working with pandas_datareader, so we're using
yfinanceinstead (install it first withpip install yfinance). - Swapped
iplot()forpy_offline.show():iplot()is for Jupyter embedding, whileshow()generates an HTML file and pops it open in your default browser. - Dropped
init_notebook_mode(): You don't need this for standalone scripts—it's only for Jupyter. - Added layout improvements: Makes the chart easier to read at a glance.
Solution 2: Native Desktop Pop-Up (No Browser Required)
If you want a true desktop app-style pop-up (not just a browser window), we can use Dash (Plotly's web framework) paired with a tiny Tkinter trigger to launch it as a "windowed" app.
Code Implementation
import dash from dash import dcc, html import plotly.graph_objs as go import yfinance as yf from datetime import datetime import tkinter as tk import webbrowser from threading import Timer # Get stock data same as before start = datetime(2020, 1, 1) end = datetime(2024, 1, 1) df = yf.download("AAPL", start=start, end=end) # Build the candlestick figure candlestick = go.Candlestick( x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], name='AAPL Candlesticks' ) fig = go.Figure(data=[candlestick]) fig.update_layout(title='AAPL Stock Candlestick Chart', xaxis_title='Date', yaxis_title='Price (USD)') # Set up a minimal Dash app app = dash.Dash(__name__) app.layout = html.Div([dcc.Graph(figure=fig)]) # Function to trigger the pop-up def launch_popup(): # Tiny Tkinter window to trigger the browser (we'll close it automatically) root = tk.Tk() root.title("Candlestick Chart") root.geometry("100x50") # Wait 2 seconds then open the Dash local server in a browser Timer(2.0, lambda: webbrowser.open('http://127.0.0.1:8050')).start() # Close the tiny Tkinter window after 3 seconds root.after(3000, root.destroy) root.mainloop() if __name__ == '__main__': # Start Dash server in the background Timer(1.0, app.run_server, kwargs={'debug': False, 'use_reloader': False}).start() # Launch the pop-up trigger launch_popup()
Notes:
- Install dependencies first:
pip install dash yfinance - This starts a local Dash server (runs in the background) and opens it in your browser as a separate window. If you want a fully native desktop window (no browser), you'd need to use something like PyQt to embed the Plotly chart, but this is a great balance of simplicity and "native" feel.
Quick Troubleshooting
- Data source issues: If yfinance isn't working, try using the Stooq data source with pandas_datareader:
import pandas_datareader.data as web df = web.DataReader("AAPL", 'stooq') - Pop-up blocked: Make sure your browser allows pop-ups from local files/localhost.
内容的提问来源于stack exchange,提问作者Jeremie
相关产品推荐
相关产品推荐

