#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Dashboard 渲染器 - 为 product-research 生成可视化看板 修复版本 v3.1 - 适配实际 data.json 数据结构 使用方式: from scripts.render_dashboard import DashboardRenderer renderer = DashboardRenderer() renderer.render("path/to/data.json") 输出: dashboard.html - 可在浏览器中直接打开的交互式看板 """ import os import json from datetime import datetime from pathlib import Path from typing import Dict, List, Any, Optional class DashboardRenderer: """Dashboard 渲染器""" @staticmethod def validate_analysis_data(data: Dict[str, Any]) -> tuple[bool, List[str]]: """ 验证分析数据完整性 返回: (is_complete, missing_fields) - is_complete: 数据是否完整 - missing_fields: 缺失的字段列表 """ missing = [] # 检查决策评估数据 decision = data.get('decision', data.get('go_nogo', {})) if not decision.get('overall_score') and not decision.get('total_score'): missing.append('decision.overall_score') # 检查 VOC 分析数据 voc = data.get('voc_analysis', {}) if not voc.get('dimensions'): missing.append('voc_analysis.dimensions') # 检查壁垒评估数据 barriers = data.get('barriers', []) if not barriers or (isinstance(barriers, list) and len(barriers) == 0): missing.append('barriers') # 检查交叉分析数据 cross = data.get('cross_analysis', {}) if not cross.get('price_type_matrix'): missing.append('cross_analysis.price_type_matrix') is_complete = len(missing) == 0 return is_complete, missing # HTML 模板 HTML_TEMPLATE = """ {{CATEGORY}} - {{SITE}} 选品分析看板

{{CATEGORY}} 选品分析看板

站点: {{SITE}} | 分析日期: {{DATA_DATE}}
选品决策评估
{{GOGO_VERDICT}}
{{GOGO_SCORE}}
{{GOGO_DETAIL}}
市场概况
{{KPI_CARDS}}
产品维度分布
{{DIMENSION_TABLES}}
{{CROSS_ANALYSIS_SECTION}} {{VOC_SECTION}} {{COMPETITOR_SECTION}} {{BARRIERS_SECTION}}
""" def __init__(self): """初始化渲染器""" self.template = self.HTML_TEMPLATE def _load_data(self, data_path: str) -> Dict[str, Any]: """加载分析数据""" with open(data_path, 'r', encoding='utf-8') as f: return json.load(f) def _render_kpi_cards(self, market_overview: Dict) -> str: """渲染 KPI 卡片""" kpis = [ ('Top100 月销量', f"{market_overview.get('top100_monthly_sales', 0):,}", '件'), ('Top100 月销额', f"${market_overview.get('top100_monthly_revenue', 0):,.0f}", ''), ('平均价格', f"${market_overview.get('avg_price', 0):.2f}", ''), ('Top3 品牌集中度', f"{market_overview.get('top3_brand_concentration', 0)*100:.1f}%", ''), ('亚马逊自营份额', f"{market_overview.get('amazon_owned_share', 0)*100:.1f}%", ''), ('中国卖家份额', f"{market_overview.get('chinese_seller_share', 0)*100:.1f}%", ''), ] cards_html = [] for label, value, note in kpis: cards_html.append(f'''
{label}
{value}
{note}
''') return ''.join(cards_html) def _render_dimension_tables(self, data: Dict) -> str: """渲染产品维度分布表格 - 适配实际数据结构""" # 从价格区间和产品类型数据生成表格 price_ranges = data.get('price_ranges', []) product_types = data.get('product_types', []) dimensions = data.get('dimensions', []) tables_html = '
' # 价格区间表格 if price_ranges: total = sum(p.get('count', 0) for p in price_ranges) rows = '' for pr in price_ranges: range_label = pr.get('range', '') count = pr.get('count', 0) share = pr.get('share', 0) * 100 if share >= 30: tag_class = 'tag-blue' elif share >= 20: tag_class = 'tag-green' elif share >= 10: tag_class = 'tag-yellow' else: tag_class = 'tag-gray' rows += f'{range_label}{count}{share:.0f}%' tables_html += f'''

价格区间分布

{rows}
价格区间 产品数 占比
''' # 产品类型表格 if product_types: total = sum(pt.get('count', 0) for pt in product_types) rows = '' for pt in product_types: type_name = pt.get('type', '') count = pt.get('count', 0) share = pt.get('share', 0) * 100 if share >= 30: tag_class = 'tag-blue' elif share >= 20: tag_class = 'tag-green' elif share >= 10: tag_class = 'tag-yellow' else: tag_class = 'tag-gray' rows += f'{type_name}{count}{share:.0f}%' tables_html += f'''

产品类型分布

{rows}
产品类型 产品数 占比
''' tables_html += '
' return tables_html def _render_cross_analysis(self, data: Dict) -> str: """渲染交叉分析部分 - 使用实际数据结构""" cross_analysis = data.get('cross_analysis', {}) price_type_matrix = cross_analysis.get('price_type_matrix', []) if not price_type_matrix: return '' # 从矩阵数据中提取列(产品类型) product_types = set() price_ranges = [] for row in price_type_matrix: price_range = row.get('price_range', '') if price_range not in price_ranges: price_ranges.append(price_range) # 获取除 price_range 外的所有键作为产品类型 for key in row.keys(): if key != 'price_range': product_types.add(key) product_types = sorted(list(product_types)) # 生成表头 header_cols = ''.join(f'{pt}' for pt in product_types) # 生成表格行 rows = '' for price_range in price_ranges: # 找到对应的价格区间行 row_data = next((r for r in price_type_matrix if r.get('price_range') == price_range), {}) row_cells = '' row_total = 0 for pt in product_types: count = row_data.get(pt, 0) row_total += count # 标记特殊值 if count == 0: cell_content = '0' elif count >= 15: cell_content = f'{count} 红海' else: cell_content = str(count) row_cells += f'{cell_content}' rows += f'{price_range}{row_cells}{row_total}' # 计算列合计 col_totals = [] for pt in product_types: col_total = sum(r.get(pt, 0) for r in price_type_matrix) col_totals.append(col_total) total_row = ''.join(f'{t}' for t in col_totals) grand_total = sum(col_totals) # 获取机会和红海信息 opportunities = cross_analysis.get('opportunities', []) red_ocean = cross_analysis.get('red_ocean', []) insight_text = [] if red_ocean: for ro in red_ocean: insight_text.append(f"红海: {ro.get('combination', '')} ({ro.get('products', 0)}款)") if opportunities: for opp in opportunities: insight_text.append(f"机会: {opp.get('combination', '')} ({opp.get('status', '')})") insight_html = f'
洞察:{" | ".join(insight_text)}
' if insight_text else '' return f'''
价格区间 × 产品类型 交叉分析
{header_cols} {rows} {total_row}
价格区间 \\ 产品类型合计
合计{grand_total}
{insight_html}
''' def _render_voc_analysis(self, voc_analysis: Dict) -> str: """渲染 VOC 分析""" if not voc_analysis or not voc_analysis.get('dimensions'): return '' dimensions = voc_analysis.get('dimensions', []) summary = voc_analysis.get('summary', '') rows = '' for dim in dimensions: dimension = dim.get('dimension', '') pain_point = dim.get('pain_point', '') frequency = dim.get('frequency', 0) percentage = dim.get('percentage', '') affected_brands = ', '.join(dim.get('affected_brands', [])) brand_opportunity = dim.get('brand_opportunity', '') product_solution = dim.get('product_solution', '') rows += f''' {dimension} {pain_point} {frequency}
{percentage} {affected_brands} {brand_opportunity} {product_solution} ''' summary_html = f'
总结:{summary}
' if summary else '' return f'''
VOC 分析 - 用户痛点洞察
{rows}
维度 痛点 频次/占比 涉及品牌 品牌机会 产品方案
{summary_html}
''' def _render_competitors(self, competitors: List[Dict]) -> str: """渲染竞品格局 - 适配实际数据结构""" if not competitors: return '' rows = '' for comp in competitors[:8]: asin = comp.get('asin', '') brand = comp.get('brand', '') model = comp.get('model', '') price = comp.get('price', 0) monthly_sales = comp.get('monthly_sales', 0) type_label = comp.get('type', '') positioning = comp.get('positioning', '') rows += f''' {brand}
{model} ${price:.2f} {monthly_sales:,} {type_label} {positioning} ''' return f'''
竞品格局 (Top8)
{rows}
品牌/型号 价格 月销量 类型 定位
''' def _render_barriers(self, barriers: List[Dict]) -> str: """渲染进入壁垒 - 适配实际数据结构""" if not barriers: return '' rows = '' for barrier in barriers: barrier_type = barrier.get('type', '') level = barrier.get('level', '中') level_class = {'高': 'tag-red', '中': 'tag-yellow', '低': 'tag-green'}.get(level, 'tag-gray') cost = barrier.get('estimated_cost', 'N/A') data_anchor = barrier.get('data_anchor', '') solution = barrier.get('solution', '') rows += f''' {barrier_type} {level} {cost} {data_anchor} {solution} ''' return f'''
进入壁垒评估
{rows}
壁垒类型 等级 预估成本 数据锚点 缓解方案
''' def render(self, data_path: str, output_path: Optional[str] = None) -> str: """ 渲染 Dashboard Args: data_path: data.json 文件路径 output_path: 输出 HTML 文件路径,默认与 data.json 同目录 Returns: str: 生成的 HTML 文件路径 """ # 加载数据 data = self._load_data(data_path) # 验证分析数据完整性 is_complete, missing = self.validate_analysis_data(data) if not is_complete: print(f"⚠️ 数据不完整,缺少分析字段: {', '.join(missing)}") print(f"ℹ️ 将渲染基础版本(不含分析结论)") print(f"ℹ️ 完成分析后请使用 --final 参数重新渲染") # 解析数据 - 适配实际结构 metadata = data.get('metadata', {}) market_overview = data.get('market_overview', {}) # 兼容新旧字段名:decision 或 go_nogo decision = data.get('decision', data.get('go_nogo', {})) competitors = data.get('competitors', []) voc_analysis = data.get('voc_analysis', {}) barriers = data.get('barriers', []) # 解析数据 - 适配实际结构 metadata = data.get('metadata', {}) market_overview = data.get('market_overview', {}) # 兼容新旧字段名:decision 或 go_nogo decision = data.get('decision', data.get('go_nogo', {})) competitors = data.get('competitors', []) voc_analysis = data.get('voc_analysis', {}) barriers = data.get('barriers', []) # 确定输出路径 if output_path is None: data_dir = Path(data_path).parent output_path = data_dir / 'dashboard.html' # 准备模板变量 # 兼容新旧字段名:keyword 或 category 或 category_name category = metadata.get('category', metadata.get('category_name', metadata.get('keyword', 'Unknown'))) site = metadata.get('site', 'US') data_date = metadata.get('date', datetime.now().strftime('%Y-%m-%d')) # Go/No-Go 样式映射 # 兼容新旧字段名:verdict 或 decision verdict = decision.get('verdict', decision.get('decision', '暂缓观望')) # 兼容新旧字段名:overall_score 或 total_score score = decision.get('overall_score', decision.get('total_score', 0)) verdict_mapping = { '建议进入': '#22c55e, #16a34a', '谨慎进入': '#f59e0b, #d97706', '暂缓观望': '#64748b, #475569', '不建议进入': '#ef4444, #dc2626', } go_gradient = verdict_mapping.get(verdict, '#64748b, #475569') # 决策详情 - 兼容新旧字段名 scores = decision.get('dimensions', decision.get('scores', [])) decision_basis = decision.get('decision_text', decision.get('decision_basis', '')) verdict_detail = decision_basis if decision_basis else verdict # KPI 卡片 kpi_cards = self._render_kpi_cards(market_overview) # 维度分布表格 dimension_tables = self._render_dimension_tables(data) # 交叉分析 cross_section = self._render_cross_analysis(data) # VOC 分析 voc_section = self._render_voc_analysis(voc_analysis) # 竞品格局 competitor_section = self._render_competitors(competitors) # 进入壁垒 barriers_section = self._render_barriers(barriers) # 渲染模板 html = self.template.replace('{{CATEGORY}}', category) \ .replace('{{SITE}}', site) \ .replace('{{DATA_DATE}}', data_date) \ .replace('{{GENERATED_TIME}}', datetime.now().strftime('%Y-%m-%d %H:%M')) \ .replace('{{GO_GRADIENT}}', go_gradient) \ .replace('{{GOGO_VERDICT}}', verdict) \ .replace('{{GOGO_SCORE}}', f'{score:.1f} / 10') \ .replace('{{GOGO_DETAIL}}', verdict_detail) \ .replace('{{KPI_CARDS}}', kpi_cards) \ .replace('{{DIMENSION_TABLES}}', dimension_tables) \ .replace('{{CROSS_ANALYSIS_SECTION}}', cross_section) \ .replace('{{VOC_SECTION}}', voc_section) \ .replace('{{COMPETITOR_SECTION}}', competitor_section) \ .replace('{{BARRIERS_SECTION}}', barriers_section) # 写入文件 output_path = Path(output_path) output_path.parent.mkdir(parents=True, exist_ok=True) with open(output_path, 'w', encoding='utf-8') as f: f.write(html) return str(output_path) # ============================================================================ # 命令行接口 # ============================================================================ if __name__ == "__main__": import argparse import sys parser = argparse.ArgumentParser( description="Dashboard 渲染器 - 为选品分析生成可视化看板", epilog=""" 示例: # 渲染 Dashboard(自动检测数据完整性) python render_dashboard.py data.json # 检查数据完整性(不生成文件) python render_dashboard.py data.json --check # 指定输出路径 python render_dashboard.py data.json -o output/dashboard.html """ ) parser.add_argument("data", help="data.json 文件路径") parser.add_argument("-o", "--output", help="输出 HTML 文件路径") parser.add_argument("--check", action="store_true", help="仅检查数据完整性,不生成文件") args = parser.parse_args() renderer = DashboardRenderer() # 仅检查模式 if args.check: data = renderer._load_data(args.data) is_complete, missing = renderer.validate_analysis_data(data) if is_complete: print("✓ 数据完整,可以渲染完整版 Dashboard") print(f" 包含: decision.overall_score, voc_analysis.dimensions, barriers, cross_analysis") sys.exit(0) else: print("⚠️ 数据不完整,缺少以下字段:") for field in missing: print(f" - {field}") print(f"\nℹ️ 请先完成 LLM 分析,然后重新运行渲染") sys.exit(1) # 渲染模式 output_path = renderer.render(args.data, args.output) print(f"✓ Dashboard 已生成: {output_path}")