Files
reddit_stock_analyzer/rstat_tool/dashboard.py

61 lines
1.9 KiB
Python

# rstat_tool/dashboard.py
from flask import Flask, render_template
# --- FIX #1: Import the new function we need ---
from .database import (
get_overall_summary,
get_subreddit_summary,
get_all_scanned_subreddits,
get_deep_dive_details
)
app = Flask(__name__, template_folder='../templates')
@app.template_filter('format_mc')
def format_market_cap(mc):
"""Formats a large number into a readable market cap string."""
if mc is None or mc == 0:
return "N/A"
if mc >= 1e12:
return f"${mc/1e12:.2f}T"
elif mc >= 1e9:
return f"${mc/1e9:.2f}B"
elif mc >= 1e6:
return f"${mc/1e6:.2f}M"
else:
return f"${mc:,}"
@app.context_processor
def inject_subreddits():
"""Makes the list of all scanned subreddits available to every template."""
subreddits = get_all_scanned_subreddits()
return dict(subreddits=subreddits)
@app.route("/")
def index():
"""The handler for the main dashboard page."""
tickers = get_overall_summary(limit=10)
return render_template("index.html", tickers=tickers)
@app.route("/subreddit/<name>")
def subreddit_dashboard(name):
"""A dynamic route for per-subreddit dashboards."""
tickers = get_subreddit_summary(name, limit=10)
return render_template("subreddit.html", tickers=tickers, subreddit_name=name)
@app.route("/deep-dive/<symbol>")
def deep_dive(symbol):
"""The handler for the deep-dive page for a specific ticker."""
# --- FIX #2: Call the function directly, without the 'database.' prefix ---
posts = get_deep_dive_details(symbol)
return render_template("deep_dive.html", posts=posts, symbol=symbol)
def start_dashboard():
"""The main function called by the 'rstat-dashboard' command."""
print("Starting Flask server...")
print("Open http://127.0.0.1:5000 in your browser.")
print("Press CTRL+C to stop the server.")
app.run(debug=True)
if __name__ == "__main__":
start_dashboard()