I'm trying to get data from websocket to flask in real-time, I didn't find a way to do it so I tried making a different route "/" and send it again to another route which is "/get" but it doesn't work cause I only can return the whole index.html file but I need only the data in realtime
<!doctype html>
<html>
<head>
<title>Flask-Sock Demo</title>
</head>
<body onload="a()">
<div id="log"></div>
<br>
<script>
const log = (text, color) => {
document.getElementById('log').innerHTML += <span style="color: ${color}">${text}</span><br>;
};
socket = new WebSocket('ws://' + "localhost:8000");
socket.addEventListener('message', ev => {
log( ev.data, 'blue');
saveDynamicDataToFile(ev.data);
console.log(ev.data);
});
function a(){
console.log("alert!")
const textField = "hello"
socket.onopen = () => socket.send(textField.value);
}
</script>
</body>
</html>
import asyncio
import websockets
from flask import Flask, render_template, request
from flask_sock import Sock
app = Flask(__name__,template_folder='template')
sock = Sock(app)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/get', methods=['GET', 'POST'])
def get():
return
if __name__ == '__main__':
app.debug = True
app.run(use_reloader=False)