I am having an issue with my flask webapp that i am trying to run but no matter what there is a 500 error. Here is my webapp:
import redis, sqlite3, time
from flask import Flask, render_template, request, g, current_app
from sqlite3 import DatabaseError
app = Flask(__name__)
r = redis.Redis(host='localhost', port=6379, db=0)
conn = sqlite3.connect('trade.db')
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS signals (
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
ticker,
order_action,
order_contracts,
order_price
)
""")
conn.commit()
def get_db():
if 'db' not in g:
g.db = sqlite3.connect('trade.db')
g.db.row_factory = sqlite3.Row
return g.db
@app.get('/')
def dashboard():
db = get_db()
cursor = db.cursor()
cursor.execute("""
SELECT * FROM signals
""")
signals = cursor.fetchall()
return render_template('dashboard.html', signals=signals)
try:
r=redis.Redis(host='localhost', port=6379, db=0)
except redis.RedisError as e:
print(f"Error: Could not connect to the Redis server")
@app.post("/webhook")
def webhook():
data = request.data
if data:
r.publish('tradingview', data)
data_dict = request.json
db = get_db()
cursor = db.cursor()
cursor.execute("""
INSERT INTO signals (ticker, order_action, order_contracts, order_price)
VALUES (?, ?, ?, ?)
""", (data_dict['ticker'],
data_dict['strategy']['order_action'],
data_dict['strategy']['order_contracts'],
data_dict['strategy']['order_price']))
db.commit()
return data
return {
"code": "success"
}
@app.errorhandler(DatabaseError)
def special_exception_handler(error):
return 'Database connection failed', 500
if __name__ == "__main__":
app.run(debug=True)
And here is my template:
<html>
<head>
<title>dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Signals</h1>
<table class="table">
<tr>
<th>timestamp</th>
<th>ticker</th>
<th>action</th>
<th>quantity</th>
<th>price</th>
</tr>
{% for signal in signals %}
<tr>
<td>{{ signal.timestamp }}</td>
<td>{{ signal.ticker }}</td>
<td>{{ signal.order_action }}</td>
<td>{{ signal.order_contracts }}</td>
<td>{{ signal.order_price }}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
This is the error log:
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
[2024-01-05 20:14:16,117] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 869, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 867, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 852, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\Users\lenovo\Desktop\IB TV Automated Trading\webapp.py", line 37, in dashboard
return render_template('dashboard.html', signals=signals)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\templating.py", line 151, in render_template
template = app.jinja_env.get_or_select_template(template_name_or_list)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\jinja2\environment.py", line 1081, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\jinja2\environment.py", line 1010, in get_template
return self._load_template(name, globals)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\jinja2\environment.py", line 969, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\jinja2\loaders.py", line 126, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\templating.py", line 65, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\templating.py", line 99, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: dashboard.html
127.0.0.1 - - [05/Jan/2024 20:14:16] "GET / HTTP/1.1" 500 -
This is the error: 500 HTTP error
Thank you for your attention.
I tried an error handling that the flask documentation recommended with the "DatabaseError" and I want this issue to be resolved so i can run my webapp
Your error log is indicating that the issue occurs because your template "dashboard.html" is not found:
Are you sure that this file is at the location that it is supposed to be?
If you didn't specify any folders explicitly, then they are supposed to go in the
templatesdirectory.