https://i.stack.imgur.com/xJ0cd.png
This is what it should look like.
from flask import Flask, redirect, url_for, abort, request, render_template
from threading import Thread
import json
import os
app = Flask('', template_folder="Template")
with open("data.json", "r+") as dataF:
data = dataF.read()
print(data)
dataF.close()
@app.route("/", methods=["GET", "POST"])
def message():
if request.method == "POST":
if request.json != None:
msg = request.json["msg"]
user = request.json["author"]
msgId = request.json["msgId"]
finishedMsg = '{"msg":"'+msg+'","author":"'+user+'","id":'+msgId+'}'
print(finishedMsg)
with open("data.json", "w+") as d:
json.dump(finishedMsg, d)
with open("Template/index.html", "w+") as html:
html.write("<!DOCTYPE html><html><h1>"+finishedMsg+"</h1></html>")
return render_template("index.html", title="page", jsonfile=json.dumps(data))
def run():
app.run(host='0.0.0.0', port="8000", extra_files="Template/index.html")
def keep_alive():
t = Thread(target=run)
t.start()
Here is the main file, it runs the website with flask. It is responsible for changing the JSON file and updating the HTML for the website but, doesn't seem to work for either.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="UTF-8" />
<title>House</title>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
crossorigin="anonymous"
/>
</head>
<body>
<div class="container"></div>
<script>
const jsonfile = JSON.parse();
console.log(jsonfile);
document.querySelector(".container").innerHTML = JSON.stringify(jsonfile, null, 2);
</script>
</body>
</html>
Here is the HTML script, any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire