Files
oe-html-viewer/main.py

110 lines
3.1 KiB
Python

import os
from flask import Flask, Response
import threading
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
# -------------- VARIABLES --------------
html_file_path = r"PATH"
host = "0.0.0.0"
port = 80
is_oe11 = True # if using OE11 set it to True, for OE12 set it to False (encoding)
scroll_pixels = 100
scroll_interval = 3 # seconds
bottom_wait_time = 3 # seconds
top_wait_time = 3 # seconds
# -------------- END VARIABLES --------------
app = Flask(__name__)
file_change_event = threading.Event()
class FileChangeHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path == html_file_path:
file_change_event.set()
@app.route("/_file_change")
def file_change_stream():
def stream():
while True:
file_change_event.wait()
yield "data: reload\n\n"
file_change_event.clear()
return Response(stream(), content_type="text/event-stream")
@app.route("/")
def serve_html():
with open(html_file_path, "r", encoding=("cp1252" if is_oe11 else "utf-8")) as file:
content = file.read()
content += f"""
<style>
html {{
scroll-behavior: smooth;
}}
</style>
<script>
let scrollAmount = {scroll_pixels};
let scrollInterval = {scroll_interval} * 1000;
let bottomWaitTime = {bottom_wait_time} * 1000;
let topWaitTime = {top_wait_time} * 1000;
let scrollPos = 0;
let scrollingDown = true;
function autoScroll() {{
if (scrollingDown) {{
window.scrollBy(0, scrollAmount);
scrollPos += scrollAmount;
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {{
scrollingDown = false;
setTimeout(reloadPage, bottomWaitTime);
return;
}}
}} else {{
window.scrollTo(0, 0);
scrollPos = 0;
scrollingDown = true;
setTimeout(autoScroll, topWaitTime);
return;
}}
setTimeout(autoScroll, scrollInterval);
}}
function reloadPage() {{
window.scrollTo(0, 0);
setTimeout(() => {{
location.reload();
}}, 100);
}}
window.onload = function() {{
window.scrollTo(0, 0);
if (document.body.scrollHeight <= window.innerHeight) {{
const eventSource = new EventSource('/_file_change');
eventSource.onmessage = function() {{
location.reload();
}};
}} else {{
setTimeout(autoScroll, topWaitTime);
}}
}};
</script>
"""
return content
if __name__ == "__main__":
event_handler = FileChangeHandler()
observer = Observer()
observer.schedule(
event_handler, path=os.path.dirname(html_file_path), recursive=False
)
observer.start()
try:
app.run(host=host, port=port)
except KeyboardInterrupt:
observer.stop()
observer.join()