PDF转交互式仪表盘提示词
来自 Wikiprompt,自由的提示词百科全书
PDF转交互式仪表盘提示词 一个使用Plotly Dash生成交互式仪表板的Python脚本提示,该仪表板可从PDF或数据文件创建,包含图表、筛选器和动态功能。
提示词内容收藏
🌐
"""
Interactive Dashboard for Key Figures Evolution
Author: Data Visualization Specialist
Description: A modern, interactive dashboard built with Plotly Dash to visualize
key financial and operational metrics from PDF documents, featuring dynamic
filters, hover effects, and comparative analysis.
"""
import dash
from dash import dcc, html, Input, Output, callback
import plotly.graph_objs as go
import plotly.express as px
import pandas as pd
import numpy as np
from datetime import datetime
# ============================================================================
# DATA PREPARATION SECTION
# ============================================================================
def generate_sample_data():
"""
Generate sample data representing key figures typically found in
financial/operational PDF documents. In production, this would be
replaced with actual PDF parsing and data extraction.
"""
years = list(range(2018, 2024))
# Revenue data (in millions)
revenue = [120, 145, 168, 192, 215, 238]
# Operating costs
operating_costs = [85, 98, 112, 125, 138, 150]
# Net profit
net_profit = [35, 47, 56, 67, 77, 88]
# Customer acquisition
new_customers = [1500, 1800, 2100, 2500, 2900, 3300]
# Employee count
employees = [45, 52, 60, 68, 75, 82]
# Market share (%)
market_share = [12.5, 14.2, 15.8, 17.3, 18.9, 20.4]
# Customer satisfaction (0-100)
satisfaction = [78, 81, 83, 85, 87, 89]
# Create DataFrame
data = {
'Year': years,
'Revenue': revenue,
'Operating_Costs': operating_costs,
'Net_Profit': net_profit,
'New_Customers': new_customers,
'Employees': employees,
'Market_Share': market_share,
'Satisfaction': satisfaction
}
df = pd.DataFrame(data)
# Calculate derived metrics
df['Profit_Margin'] = (df['Net_Profit'] / df['Revenue']) * 100
df['Revenue_Growth'] = df['Revenue'].pct_change() * 100
df['Cost_Ratio'] = (df['Operating_Costs'] / df['Revenue']) * 100
return df
# Generate the dataset
df = generate_sample_data()
# ============================================================================
# DASHBOARD INITIALIZATION
# ============================================================================
# Initialize the Dash app
app = dash.Dash(__name__)
app.title = "Key Figures Dashboard"
# Define color scheme for modern look
COLORS = {
'background': '#1e1e2e',
'card': '#2d2d44',
'text': '#cdd6f4',
'accent': '#89b4fa',
'accent2': '#f38ba8',
'accent3': '#a6e3a1',
'accent4': '#fab387'
}
# ============================================================================
# LAYOUT SECTION
# ============================================================================
app.layout = html.Div(
style={
'backgroundColor': COLORS['background'],
'padding': '20px',
'fontFamily': 'Inter, system-ui, -apple-system, sans-serif'
},
children=[
# Header Section
html.Div([
html.H1(
"Key Figures Dashboard",
style={
'color': COLORS['text'],
'textAlign': 'center',
'marginBottom': '30px',
'fontWeight': '600'
}
),
html.P(
"Interactive visualization of organizational performance metrics",
style={
'color': COLORS['text'],
'textAlign': 'center',
'opacity': '0.8',
'marginBottom': '30px'
}
),
]),
# Filters Section
html.Div([
html.Label(
"Select Year Range:",
style={'color': COLORS['text'], 'marginRight': '15px'}
),
dcc.RangeSlider(
id='year-filter',
min=df['Year'].min(),
max=df['Year'].max(),
value=[df['Year'].min(), df['Year'].max()],
marks={year: str(year) for year in df['Year']},
step=1,
tooltip={"placement": "bottom", "always_visible": True},
className="range-slider"
),
], style={
'backgroundColor': COLORS['card'],
'padding': '20px',
'borderRadius': '10px',
'marginBottom': '30px'
}),
# Charts Container
html.Div(id='charts-container', children=[
# Chart 1: Revenue and Profit Evolution
html.Div([
dcc.Graph(id='revenue-profit-chart')
], style={'marginBottom': '30px'}),
# Chart 2: Cost Analysis
html.Div([
dcc.Graph(id='cost-analysis-chart')
], style={'marginBottom': '30px'}),
# Chart 3: Customer Metrics
html.Div([
dcc.Graph(id='customer-metrics-chart')
], style={'marginBottom': '30px'}),
# Chart 4: Operational Metrics
html.Div([
dcc.Graph(id='operational-metrics-chart')
], style={'marginBottom': '30px'}),
# Chart 5: Efficiency Metrics
html.Div([
dcc.Graph(id='efficiency-metrics-chart')
], style={'marginBottom': '30px'}),
]),
# Footer
html.Div(
"Data source: Financial Reports 2018-2023",
style={
'color': COLORS['text'],
'textAlign': 'center',
'opacity': '0.6',
'marginTop': '20px'
}
)
]
)
# ============================================================================
# CALLBACKS AND CHART CREATION
# ============================================================================
@callback(
[Output('revenue-profit-chart', 'figure'),
Output('cost-analysis-chart', 'figure'),
Output('customer-metrics-chart', 'figure'),
Output('operational-metrics-chart', 'figure'),
Output('efficiency-metrics-chart', 'figure')],
[Input('year-filter', 'value')]
)
def update_charts(year_range):
"""
Update all charts based on selected year range.
Args:
year_range: List containing [start_year, end_year]
Returns:
List of figure objects for each chart
"""
# Filter data based on selected years
filtered_df = df[(df['Year'] >= year_range[0]) & (df['Year'] <= year_range[1])]
# Chart 1: Revenue and Profit Evolution
fig1 = go.Figure()
# Revenue line
fig1.add_trace(go.Scatter(
x=filtered_df['Year'],
y=filtered_df['Revenue'],
name='Revenue',
mode='lines+markers',
line=dict(color=COLORS['accent'], width=3),
marker=dict(size=8, color=COLORS['accent']),
hovertemplate='Year: %{x}<br>Revenue: $%{y:.1f}M<extra></extra>'
))
# Net Profit line
fig1.add_trace(go.Scatter(
x=filtered_df['Year'],
y=filtered_df['Net_Profit'],
name='Net Profit',
mode='lines+markers',
line=dict(color=COLORS['accent2'], width=3),
marker=dict(size=8, color=COLORS['accent2']),
hovertemplate='Year: %{x}<br>Net Profit: $%{y:.1f}M<extra></extra>'
))
fig1.update_layout(
title="Revenue and Net Profit Evolution",
xaxis_title="Year",
yaxis_title="Amount (Millions $)",
template='plotly_dark',
paper_bgcolor=COLORS['card'],
plot_bgcolor=COLORS['card'],
font=dict(color=COLORS['text']),
hovermode='x unified',
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
# Chart 2: Cost Analysis
fig2 = go.Figure()
# Operating costs bar
fig2.add_trace(go.Bar(
x=filtered_df['Year'],
y=filtered_df['Operating_Costs'],
name='Operating Costs',
marker_color=COLORS['accent3'],
hovertemplate='Year: %{x}<br>Operating Costs: $%{y:.1f}M<extra></extra>'
))
# Cost ratio line
fig2.add_trace(go.Scatter(
x=filtered_df['Year'],
y=filtered_df['Cost_Ratio'],
name='Cost Ratio (%)',
mode='lines+markers',
yaxis='y2',
line=dict(color=COLORS['accent4'], width=3),
marker=dict(size=8, color=COLORS['accent4']),
hovertemplate='Year: %{x}<br>Cost Ratio: %{y:.1f}%<extra></extra>'
))
fig2.update_layout(
title="Operating Costs and Cost Ratio Analysis",
xaxis_title="Year",
yaxis_title="Amount (Millions $)",
yaxis2=dict(
title="Cost Ratio (%)",
overlaying='y',
side='right',
showgrid=False
),
template='plotly_dark',
paper_bgcolor=COLORS['card'],
plot_bgcolor=COLORS['card'],
font=dict(color=COLORS['text']),
hovermode='x unified',
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
# Chart 3: Customer Metrics
fig3 = go.Figure()
# New customers bar
fig3.add_trace(go.Bar(
x=filtered_df['Year'],
y=filtered_df['New_Customers'],
name='New Customers',
marker_color=COLORS['accent'],
hovertemplate='Year: %{x}<br>New Customers: %{y:,}<extra></extra>'
))
# Customer satisfaction line
fig3.add_trace(go.Scatter(
x=filtered_df['Year'],
y=filtered_df['Satisfaction'],
name='Customer Satisfaction',
mode='lines+markers',
yaxis='y2',
line=dict(color=COLORS['accent2'], width=3),
marker=dict(size=8, color=COLORS['accent2']),
hovertemplate='Year: %{x}<br>Satisfaction Score: %{y:.1f}/100<extra></extra>'
))
fig3.update_layout(
title="Customer Acquisition and Satisfaction",
xaxis_title="Year",
yaxis_title="New Customers",
yaxis2=dict(
title="Satisfaction Score",
overlaying='y',
side='right',
range=[0, 100],
showgrid=False
),
template='plotly_dark',
paper_bgcolor=COLORS['card'],
plot_bgcolor=COLORS['card'],
font=dict(color=COLORS['text']),
hovermode='x unified',
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
# Chart 4: Operational Metrics
fig4 = go.Figure()
# Employees area chart
fig4.add_trace(go.Scatter(
x=filtered_df['Year'],
y=filtered_df['Employees'],
name='Employees',
mode='lines+markers',
fill='tozeroy',
line=dict(color=COLORS['accent3'], width=3),
marker=dict(size=8, color=COLORS['accent3']),
hovertemplate='Year: %{x}<br>Employees: %{y}<extra></extra>'
))
# Market share line
fig4.add_trace(go.Scatter(
x=filtered_df['Year'],
y=filtered_df['Market_Share'],
name='Market Share (%)',
mode='lines+markers',
yaxis='y2',
line=dict(color=COLORS['accent4'], width=3, dash='dash'),
marker=dict(size=8, color=COLORS['accent4']),
hovertemplate='Year: %{x}<br>Market Share: %{y:.1f}%<extra></extra>'
))
fig4.update_layout(
title="Workforce and Market Presence",
xaxis_title="Year",
yaxis_title="Number of Employees",
yaxis2=dict(
title="Market Share (%)",
overlaying='y',
side='right',
showgrid=False
),
template='plotly_dark',
paper_bgcolor=COLORS['card'],
plot_bgcolor=COLORS['card'],
font=dict(color=COLORS['text']),
hovermode='x unified',
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
# Chart 5: Efficiency Metrics
fig5 = go.Figure()
# Profit margin
fig5.add_trace(go.Bar(
x=filtered_df['Year'],
y=filtered_df['Profit_Margin'],
name='Profit Margin (%)',
marker_color=COLORS['accent'],
hovertemplate='Year: %{x}<br>Profit Margin: %{y:.1f}%<extra></extra>'
))
# Revenue growth
fig5.add_trace(go.Scatter(
x=filtered_df['Year'],
y=filtered_df['Revenue_Growth'],
name='Revenue Growth (%)',
mode='lines+markers',
yaxis='y2',
line=dict(color=COLORS['accent2'], width=3),
marker=dict(size=8, color=COLORS['accent2']),
hovertemplate='Year: %{x}<br>Revenue Growth: %{y:.1f}%<extra></extra>'
))
fig5.update_layout(
title="Profitability and Growth Metrics",
xaxis_title="Year",
yaxis_title="Profit Margin (%)",
yaxis2=dict(
title="Revenue Growth (%)",
overlaying='y',
side='right',
showgrid=False
),
template='plotly_dark',
paper_bgcolor=COLORS['card'],
plot_bgcolor=COLORS['card'],
font=dict(color=COLORS['text']),
hovermode='x unified',
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
return fig1, fig2, fig3, fig4, fig5
# ============================================================================
# MAIN EXECUTION
# ============================================================================
if __name__ == '__main__':
# Run the app - compatible with Replit environment
app.run_server(
host='0.0.0.0', # Allows access from any network interface
port=8050, # Default Plotly Dash port
debug=True # Enable debug mode for development
)
用法
此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。
为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。
讨论
0 条评论