add form + ajax code

This commit is contained in:
GroovieGermanikus 2023-10-27 19:48:14 +02:00
parent 9f6e9a8f10
commit 433dae390d
No known key found for this signature in database
GPG Key ID: 5B6EB831A5CD2015
6 changed files with 115 additions and 3 deletions

41
app.py
View File

@ -1,7 +1,8 @@
from flask import Flask, render_template
from flask import Flask, render_template, request, make_response
from turbo_flask import Turbo
import threading
import time
from flask_htmx import HTMX
import transaction_database
import recent_blocks_database
@ -14,6 +15,8 @@ import config
webapp = Flask(__name__)
# https://blog.miguelgrinberg.com/post/dynamically-update-your-flask-web-pages-using-turbo-flask
turbo = Turbo(webapp)
htmx = HTMX(webapp)
webapp.update_thread_started = False
@ -49,6 +52,42 @@ def recent_blocks():
return render_template('recent_blocks.html', config=this_config, blocks=maprows)
@webapp.route("/searchzzzz", methods=["GET", "POST"])
def fooobar():
if request.method == "POST":
data = dict(request.form)
print("search", data["search"])
users = getusers(data["search"])
else:
users = []
print(users)
return render_template("search.html", usr=users)
@webapp.route('/search', methods=["GET", "POST"])
def search2():
if htmx:
print("AJAX request")
return render_template('_searchresult.html')
return render_template('searchajax.html')
# uid INTEGER,
# name TEXT NOT NULL,
# email TEXT NOT NULL,
# tel TEXT NOT NULL,
def getusers(search):
row = dict()
row["uid"] = 42
row["name"] = "John, Doe"
row["email"] = "foo@bar.com"
row["tel"] = "0121212"
results = [row, row ,row]
return results
def start_if_needed():
if webapp.update_thread_started:
return

View File

@ -66,7 +66,7 @@ def run_query():
total_cu_requested
FROM banking_stage_results.blocks
-- this critera uses index idx_blocks_slot_errors
WHERE banking_stage_errors > 0
WHERE true
ORDER BY slot DESC
LIMIT 30
) AS data

View File

@ -1,4 +1,18 @@
Flask==3.0.0
asn1crypto==1.5.1
blinker==1.6.3
click==8.1.7
Flask==2.3.3
flask-htmx==0.3.1
flask-sock==0.7.0
h11==0.14.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
pg8000==1.30.2
python-dateutil==2.8.2
scramp==1.4.4
simple-websocket==1.0.0
six==1.16.0
Turbo-Flask==0.8.4
Werkzeug==3.0.1
wsproto==1.2.0

View File

@ -0,0 +1,5 @@
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>

18
templates/search.html Normal file
View File

@ -0,0 +1,18 @@
<form method="post">
<input type="text" name="search" required>
<input type="submit" value="Search">
</form>
<!-- (B) OUTPUT SEARCH RESULTS -->
{% if usr | length != 0 %}
<table id="demo">
{% for u in usr %}
<tr>
<td>{{ u.name }}</td>
<td>{{ u.email }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<div id="demo">No search results.</div>
{% endif %}

36
templates/searchajax.html Normal file
View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/htmx.org@1.9.6" integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni" crossorigin="anonymous"></script>
</head>
<body>
<h3>
Search Contacts
<span class="htmx-indicator">
<img src="/img/bars.svg"/> Searching...
</span>
</h3>
<input class="form-control" type="search"
name="search" placeholder="Begin Typing To Search Users..."
hx-post="/search"
hx-trigger="keyup changed delay:500ms, search"
hx-target="#search-results"
hx-indicator=".htmx-indicator">
<table class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody id="search-results">
</tbody>
</table>
</body>
</html>