diff --git a/main.py b/main.py index 6509069..b435a53 100644 --- a/main.py +++ b/main.py @@ -368,23 +368,52 @@ def custom(): return render_template('custom.html', message='No') -@app.route('/result') +@app.route('/result', methods=['GET', 'POST']) def result(): if login_required(): pass else: flash('使用投組功能請先登入。', 'warning') return redirect(url_for('login')) - - sql="""select id, date, name, username, annual_ret, vol, annual_sr, mdd\ - from strategy order by id desc limit 50;""" - conn = psycopg2.connect(**SQL_CONFIG) - with conn: - with conn.cursor() as curs: - curs.execute(sql) - data= curs.fetchall() - conn.close() - return render_template('result.html', strategy_data=data) + if request.method=='GET': + conn = psycopg2.connect(**SQL_CONFIG) + with conn: + with conn.cursor() as curs: + sql="select id, date, name, username, annual_ret, vol, annual_sr, mdd\ + from strategy order by id desc limit 50" + curs.execute(sql) + data= curs.fetchall() + conn.close() + return render_template('result.html', strategy_data=data) + elif request.method=='POST': + role = request.form.get('role') + comp = request.form.get('competition') + if role in ['id', 'annual_ret', 'annual_sr', 'volatility']: + pass + else: + role='id' + if comp == 'none': + comp=None + print("result", type(role), type(comp)) + conn = psycopg2.connect(**SQL_CONFIG) + with conn: + with conn.cursor() as curs: + if comp is None: + if role is None: + sql="select id, date, name, username, annual_ret, vol, annual_sr, mdd\ + from strategy order by id desc limit 50" + curs.execute(sql) + else: + sql=f"select id, date, name, username, annual_ret, vol, annual_sr, mdd\ + from strategy order by {role} desc limit 50" + curs.execute(sql) + else: + sql=f"select id, date, name, username, annual_ret, vol, annual_sr, mdd\ + from strategy where competition=%s order by {role} desc limit 50;" + curs.execute(sql, (comp, )) + data= curs.fetchall() + conn.close() + return render_template('result.html', strategy_data=data) @app.route('/result_view') def result_view(): @@ -428,10 +457,10 @@ def result_view(): data['bar'] = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) return render_template('result_view.html', data=data) -# handle login failed -# @app.errorhandler(401) -# def page_not_found(e): -# return response('
Failed
') +@app.errorhandler(404) +def page_not_found(e): + # note that we set the 404 status explicitly + return render_template('404.html'), 404 diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..17151ef --- /dev/null +++ b/templates/404.html @@ -0,0 +1,14 @@ + + + + + + +