add a settings page and can add edit instruments in setting web page
This commit is contained in:
@@ -5,9 +5,53 @@ import concurrent.futures
|
||||
from flask_caching import Cache
|
||||
import csv, os, logging
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import pandas as pd
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})
|
||||
# The CSV is in the root directory (same level as app.py)
|
||||
CSV_PATH = os.path.join(os.path.dirname(__file__), 'instruments.csv')
|
||||
|
||||
@app.route('/settings')
|
||||
def settings():
|
||||
instruments = []
|
||||
last_updated = "Never"
|
||||
|
||||
if os.path.exists(CSV_PATH):
|
||||
# 1. Get the last modified time from the Synology filesystem
|
||||
mtime = os.path.getmtime(CSV_PATH)
|
||||
last_updated = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# 2. Read the data
|
||||
df = pd.read_csv(CSV_PATH)
|
||||
df = df.fillna('')
|
||||
instruments = df.to_dict(orient='records')
|
||||
|
||||
return render_template('settings.html', instruments=instruments, last_updated=last_updated)
|
||||
|
||||
@app.route('/settings/save', methods=['POST'])
|
||||
def save_settings():
|
||||
try:
|
||||
names = request.form.getlist('name[]')
|
||||
cusips = request.form.getlist('cusip[]')
|
||||
providers = request.form.getlist('provider[]')
|
||||
|
||||
# Create a new DataFrame from the web form data
|
||||
new_data = {
|
||||
'name': [s.strip() for s in names if s.strip()],
|
||||
'cusip': [u.strip() for u in cusips if u.strip()],
|
||||
'provider': [p.strip() for p in providers if p.strip()]
|
||||
}
|
||||
|
||||
df = pd.DataFrame(new_data)
|
||||
# Save directly back to the root folder
|
||||
df.to_csv(CSV_PATH, index=False)
|
||||
|
||||
return redirect('/settings')
|
||||
except Exception as e:
|
||||
print(f"Error saving CSV: {e}")
|
||||
return "Internal Server Error", 500
|
||||
|
||||
@cache.memoize(timeout=3600)
|
||||
def fetch_and_calculate(config):
|
||||
|
||||
Reference in New Issue
Block a user