trying to create a web interface for all

This commit is contained in:
Coding with Peter 2023-07-03 07:28:17 -07:00
parent 1607762266
commit 650ad9539c
3 changed files with 131 additions and 0 deletions

View File

@ -1852,6 +1852,12 @@ WHERE
connection.close()
def test_long_running():
from time import sleep
print("Starting long process...")
for i in range(20):
print("sleeping %s" % i, flush=True)
sleep(1)
@ -1881,6 +1887,7 @@ if __name__ == "__main__":
20: ['Process enrollment data', process_enrollment_data],
21: ['Encode data', do_encoding],
22: ['all students course history', all_students_history],
23: ['test long running', test_long_running],
#19: ['add evals for a whole semester', instructor_list_to_activate_evals],
#16: ['Upload new employees to flex app', employees_refresh_flex],
}

77
myweb.py Normal file
View File

@ -0,0 +1,77 @@
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from flask_sse import sse
from threading import Thread
import subprocess
from flask import Flask, render_template
from flask_sse import sse
from threading import Thread
import subprocess
app = Flask(__name__)
app.config['REDIS_URL'] = 'redis://localhost'
app.register_blueprint(sse, url_prefix='/stream')
# Background thread to run the long-running task
def run_long_running_task():
process = subprocess.Popen(['python', 'localcache.py', '23'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
for line in process.stdout:
# Emit the output line as a server-sent event
sse.publish({'data': line.strip()}, type='output')
process.wait()
@app.route('/')
def index():
return render_template('myweb.html')
@app.route('/start')
def start_task():
# Start the long-running task in a background thread
thread = Thread(target=run_long_running_task)
thread.start()
return 'Task started'
if __name__ == '__main__':
app.run()
'''
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
socketio = SocketIO(app)
# Background process to run the long-running task
def run_long_running_task():
process = subprocess.Popen(['python', 'localcache.py', '23'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
for line in process.stdout:
# Emit the output line to the client
socketio.emit('output', {'data': line.strip()})
print(f"Sent: {line.strip()}")
print("Process is done")
process.wait()
@app.route('/')
def index():
return render_template('myweb.html')
@socketio.on('connect')
def on_connect():
# Start the long-running task when a client connects
thread = Thread(target=run_long_running_task)
thread.start()
if __name__ == '__main__':
socketio.run(app)
'''

47
templates/myweb.html Normal file
View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<title>Interactive Text Program</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.js"></script>
<script>
var socket = io();
socket.on('output', function(data) {
// Append the received text output to the page
var outputDiv = document.getElementById('output');
outputDiv.innerHTML += data.data + '<br>';
outputDiv.scrollTop = outputDiv.scrollHeight;
});
function sendInput() {
var inputField = document.getElementById('input');
var userInput = inputField.value;
// Send the user input to the server
socket.emit('input', {data: userInput});
// Clear the input field
inputField.value = '';
}
const eventSource = new EventSource('/stream');
eventSource.addEventListener('output', function (event) {
const data = JSON.parse(event.data);
// Update the web page with the received output data
// (e.g., append it to a <div> element)
});
eventSource.onerror = function () {
// Handle errors
};
</script>
</head>
<body>
<div id="output"></div>
<input type="text" id="input" onkeydown="if (event.key === 'Enter') sendInput()" autofocus>
<button onclick="sendInput()">Send</button>
</body>
</html>