Added web dashboard.
This commit is contained in:
54
rstat_tool/dashboard.py
Normal file
54
rstat_tool/dashboard.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# rstat_tool/dashboard.py
|
||||
|
||||
from flask import Flask, render_template
|
||||
from .database import (
|
||||
get_overall_summary,
|
||||
get_subreddit_summary,
|
||||
get_all_scanned_subreddits
|
||||
)
|
||||
|
||||
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."""
|
||||
# --- CHANGE HERE: Limit the data to the top 10 ---
|
||||
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."""
|
||||
# --- CHANGE HERE: Limit the data to the top 10 ---
|
||||
tickers = get_subreddit_summary(name, limit=10)
|
||||
return render_template("subreddit.html", tickers=tickers, subreddit_name=name)
|
||||
|
||||
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()
|
Reference in New Issue
Block a user