Skip to content

Commit

Permalink
add ui
Browse files Browse the repository at this point in the history
  • Loading branch information
voschezang committed Jul 5, 2023
1 parent 0f4b0b0 commit b682f63
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ upload-test:

upload:
make build
python3 -m twine upload dist/* -u "${user}" -p "${pass}"
python3 -m twine upload dist/* -u "${user}" -p "${pass}"

docs-init:
mkdir -p docs
Expand All @@ -53,3 +53,8 @@ tree:

ast:
egrep -rh '^class \w+(\(\w+\))?\:' src/mash/shell/ast

web:
open web/home.html
python3 src/examples/server.py

16 changes: 13 additions & 3 deletions src/mash/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from json import loads
from json import JSONDecodeError, loads
from logging import debug
from flask import Flask, request
from http import HTTPStatus
Expand Down Expand Up @@ -39,12 +39,18 @@ def init_db():
db = {'users': {i: generate_user(i) for i in range(10)}}


def url(path):
return f'http://127.0.0.1:5000{basepath}{path}'


def init_routes(app):
@app.route(basepath)
def root():
data = ['document', 'users']
test = ['echo', 'sleep', 'stable', 'scrambled', 'noisy']
return data + test
# return data + test
print(0)
return {k: url(k) for k in data + test}

@app.route(basepath + "echo", methods=['GET', 'POST'])
def echo():
Expand Down Expand Up @@ -132,7 +138,11 @@ def users():
return [i for i in db['users'].keys()]

if request.method == 'POST':
data = loads(request.data)
try:
print(request.data)
data = loads(request.data)
except JSONDecodeError:
return 'Payload decoding error', HTTPStatus.BAD_REQUEST

# WARNING: this exposes internal classes
try:
Expand Down
64 changes: 64 additions & 0 deletions web/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<head>

<script>

const data = { name: "alice", email: "[email protected]" };

function submit() {
const request = new Request("http://127.0.0.1:5000/v1/users", {
method: "POST",
body: JSON.stringify(data),
});

console.log("POST")
fetch(request)
.then((response) => response.json())
.then((data) => {
console.log(data)
reload()
});
}

function reload() {
const request = new Request("http://127.0.0.1:5000/v1/users", { method: "GET", });

console.log("GET")
fetch(request)
.then((response) => response.json())
.then((data) => {
document.getElementById("t1").innerHTML = data;
});
}

</script>

</head>
<body style="text-align: center;">


<button onclick="reload()">Reload</button>

<div style="background: #eee; max-width: 500px; display: inline">
<p id="t1"></p>
</div>

<div style="margin: 30px 0;">

<div style="margin: 25px 0">
<p>Name</p>
<input type="text" value="full name">

<p>Email</p>
<input type="text" value="[email protected]">
</div>

<button type="button" onclick="submit()"> Add </button>
</div>

<script>
reload()
</script>

</body>
</html>

0 comments on commit b682f63

Please sign in to comment.