generic tables from dataclasses

This commit is contained in:
null 2024-03-07 22:18:27 +01:00
parent a5c01038ca
commit f9743b834c
6 changed files with 105 additions and 0 deletions

3
app.py
View File

@ -1,11 +1,14 @@
from flask import Flask, render_template, jsonify
from routes.conditions import create_conditions_routes
from db import get_connection, getRethinkDB
from routes.create import create_routes
from models.Abc import Abc
app = Flask(__name__)
r = getRethinkDB()
create_conditions_routes(app)
create_routes(Abc, app)
@app.route('/')
def index():

18
models/Abc.py Normal file
View File

@ -0,0 +1,18 @@
from dataclasses import dataclass
from .Strategy import Strategy
@dataclass
class Abc(Strategy):
symbol: str
entry: float
stop: float
tp: float
risk: float
@staticmethod
def get_table_name():
return "Abc"
@staticmethod
def get_route_prefix():
return "/strategies/abc"

8
models/Strategy.py Normal file
View File

@ -0,0 +1,8 @@
from dataclasses import dataclass
@dataclass
class Strategy():
id: str
enabled: bool
fulfilled: bool
created: str

0
models/__init__.py Normal file
View File

25
routes/create.py Normal file
View File

@ -0,0 +1,25 @@
from db import get_connection, getRethinkDB
from dataclasses import fields
from flask import jsonify, request, redirect, render_template
r = getRethinkDB()
def create_routes(cls, app):
print("creating routes for " + cls.__name__)
table_name = cls.get_table_name()
if table_name not in r.table_list().run(get_connection()):
print("creating table " + table_name)
r.table_create(table_name).run(get_connection())
@app.route(cls.get_route_prefix() + "/", methods=['GET'])
def get():
cursor = r.table(table_name).run(get_connection())
items = list(cursor)
return render_template(cls.get_route_prefix() + "/index.html", data={
"title": cls.__name__,
"headers": [field.name for field in fields(cls)],
"items": items,
"prefix": cls.get_route_prefix()
})

View File

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trading Zone Website</title>
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">
<script type="text/javascript" src="{{ url_for('static', filename='js/main.js') }}" ></script>
</head>
<body>
<div class="container">
<table id="conditionTable">
<thead>
<tr>
<td colspan="{{ data.headers | length }}"><h2>{{ data.title }}</h2></td>
</tr>
{% if data.items %}
<tr>
{% for header in data.headers %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% else %}
<tr>
<th class="nodata">No Data</th>
</tr>
{% endif %}
</thead>
<tbody>
{% for item in data["items"] %}
<tr onclick="onOpenDialog('{{ data.prefix }}/{{ item.id }}')">
<td>{{ item }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td colspan="{{ data.headers | length }}" style="text-align: right;"> <!-- Adjust colspan as per your total columns -->
<button onclick="onOpenDialog('{{ data.prefix }}/create')">Add Item</button>
</td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>