- enable a new output option in HTML format.
This commit is contained in:
parent
b9a3a2dbcc
commit
f350b32825
73
cli/main.py
73
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 = """<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{title}</title>
|
||||
<style>
|
||||
body {{ font-family: Arial, sans-serif; margin: 40px; }}
|
||||
h1,h2,h3,h4 {{ color: #2c3e50; }}
|
||||
table {{ border-collapse: collapse; width: 100%; margin-bottom: 20px; }}
|
||||
th, td {{ border: 1px solid #ddd; padding: 8px; }}
|
||||
th {{ background-color: #f2f2f2; text-align: left; }}
|
||||
a {{ color: #3498db; text-decoration: none; }}
|
||||
a:hover {{ text-decoration: underline; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
{content}
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
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'<li><a href="{filename}">{display_name}</a></li>'
|
||||
index_body = f"{decision_html}<h2>Other Reports</h2><ul>{links}</ul>"
|
||||
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():
|
||||
|
|
|
|||
24
cli/utils.py
24
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
|
||||
|
|
|
|||
|
|
@ -24,3 +24,4 @@ rich
|
|||
questionary
|
||||
langchain_anthropic
|
||||
langchain-google-genai
|
||||
markdown
|
||||
|
|
|
|||
Loading…
Reference in New Issue