diff --git a/cli/main.py b/cli/main.py index f2f6b1bc..3400f93b 100644 --- a/cli/main.py +++ b/cli/main.py @@ -24,6 +24,40 @@ from dotenv import load_dotenv # Load API keys from .env load_dotenv() + + +# HTML output template and converter +HTML_TEMPLATE = """ + + + +{title} + + + +{content} + +""" + +def md_to_html(md_text: str) -> str: + """Convert Markdown text to HTML, dynamically importing markdown.""" + try: + import markdown + except ImportError: + raise RuntimeError( + "HTML output requires the 'markdown' package. Please install it with `pip install markdown`." + ) + return markdown.markdown(md_text, extensions=["tables", "fenced_code"]) + + from tradingagents.graph.trading_graph import TradingAgentsGraph from tradingagents.default_config import DEFAULT_CONFIG from cli.models import AnalystType @@ -484,6 +518,15 @@ def get_user_selections(): selected_shallow_thinker = select_shallow_thinking_agent(selected_llm_provider) selected_deep_thinker = select_deep_thinking_agent(selected_llm_provider) + console.print( + create_question_box( + "Step 7: Output Format", "Choose output format (Markdown or HTML)", "Markdown" + ) + ) + selected_output_format = select_output_format() + console.print(f"[green]Selected output format:[/green] {selected_output_format.upper()}") + + return { "ticker": selected_ticker, "analysis_date": analysis_date, @@ -493,6 +536,7 @@ def get_user_selections(): "backend_url": backend_url, "shallow_thinker": selected_shallow_thinker, "deep_thinker": selected_deep_thinker, + "output_format": selected_output_format, } @@ -738,6 +782,7 @@ def extract_content_string(content): def run_analysis(): # First get all user selections selections = get_user_selections() + output_format = selections["output_format"] # Create config with selected research depth config = DEFAULT_CONFIG.copy() @@ -791,9 +836,15 @@ def run_analysis(): if section_name in obj.report_sections and obj.report_sections[section_name] is not None: content = obj.report_sections[section_name] if content: - file_name = f"{section_name}.md" - with open(report_dir / file_name, "w", encoding="utf-8") as f: - f.write(content) + if output_format == "md": + file_name = f"{section_name}.md" + with open(report_dir / file_name, "w", encoding="utf-8") as f: + f.write(content) + else: + file_name = f"{section_name}.html" + html_body = md_to_html(content) + with open(report_dir / file_name, "w", encoding="utf-8") as f: + f.write(HTML_TEMPLATE.format(title=section_name.replace('_',' ').title(), content=html_body)) return wrapper message_buffer.add_message = save_message_decorator(message_buffer, "add_message") @@ -1099,6 +1150,22 @@ def run_analysis(): update_display(layout) + # Generate HTML index if HTML output selected + if output_format == "html": + # Convert final decision markdown to HTML + decision_md = message_buffer.report_sections.get("final_trade_decision", "") + decision_html = md_to_html(decision_md) + # Build links list + links = "" + for section, content in message_buffer.report_sections.items(): + if section != "final_trade_decision" and content: + filename = f"{section}.html" + display_name = section.replace("_", " ").title() + links += f'
  • {display_name}
  • ' + index_body = f"{decision_html}

    Other Reports

    " + with open(report_dir / "index.html", "w", encoding="utf-8") as f: + f.write(HTML_TEMPLATE.format(title="TradingAgents Report", content=index_body)) + @app.command() def analyze(): diff --git a/cli/utils.py b/cli/utils.py index 7b9682a6..3783756c 100644 --- a/cli/utils.py +++ b/cli/utils.py @@ -1,4 +1,6 @@ import questionary +from rich.console import Console +console = Console() from typing import List, Optional, Tuple, Dict from cli.models import AnalystType @@ -239,6 +241,28 @@ def select_deep_thinking_agent(provider) -> str: return choice +def select_output_format() -> str: + """Prompt the user to choose output format (Markdown or HTML).""" + choice = questionary.select( + "Select output format:", + choices=[ + questionary.Choice("Markdown", "md"), + questionary.Choice("HTML", "html"), + ], + instruction="\n- Use arrow keys to navigate\n- Press Enter to select", + style=questionary.Style( + [ + ("selected", "fg:cyan noinherit"), + ("highlighted", "fg:cyan noinherit"), + ("pointer", "noinherit"), + ] + ), + ).ask() + if choice is None: + console.print("\n[red]No output format selected. Exiting...[/red]") + exit(1) + return choice + def select_llm_provider() -> tuple[str, str]: """Select the OpenAI api url using interactive selection.""" # Define OpenAI api options with their corresponding endpoints diff --git a/requirements.txt b/requirements.txt index a6154cd2..7c0ef1ac 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,3 +24,4 @@ rich questionary langchain_anthropic langchain-google-genai +markdown diff --git a/setup.py b/setup.py index 793df3e6..bfc7880f 100644 --- a/setup.py +++ b/setup.py @@ -25,6 +25,7 @@ setup( "typer>=0.9.0", "rich>=13.0.0", "questionary>=2.0.1", + "markdown>=3.4.0", ], python_requires=">=3.10", entry_points={