Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toast when model reloading in progress #79

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cq_server/module_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List, Dict, Tuple
import glob
import json

import time

IGNORE_FILE_NAME = '.cqsignore'

Expand Down Expand Up @@ -185,9 +185,13 @@ def get_data(self) -> dict:

if self.module_name:
try:
start_time = time.perf_counter()
model = self.get_json_model()
end_time = time.perf_counter()
data = {
'module_name': self.module_name,
'model': self.get_json_model(),
'model': model,
'time_elapsed': end_time - start_time,
'source': ''
}
except ModuleManagerError as error:
Expand Down
6 changes: 4 additions & 2 deletions cq_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@


WATCH_PERIOD = 0.3
SSE_MESSAGE_TEMPLATE = 'event: file_update\ndata: %s\n\n'
SSE_FILE_UPDATE_MESSAGE_TEMPLATE = 'event: file_update\ndata: %s\n\n'
SSE_LOADING_MODEL_MESSAGE_TEMPLATE = 'event: loading_model\ndata: %s\n\n'


app = Flask(__name__, static_url_path='/static')
Expand Down Expand Up @@ -74,8 +75,9 @@ def watchdog() -> None:

if last_updated_file:
module_manager.module_name = op.basename(last_updated_file)[:-3]
events_queue.put(SSE_LOADING_MODEL_MESSAGE_TEMPLATE % module_manager.module_name)
data = module_manager.get_data()
events_queue.put(SSE_MESSAGE_TEMPLATE % json.dumps(data))
events_queue.put(SSE_FILE_UPDATE_MESSAGE_TEMPLATE % json.dumps(data))
sleep(WATCH_PERIOD)

events_queue = Queue(maxsize = 3)
Expand Down
18 changes: 18 additions & 0 deletions cq_server/static/viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ body {
background-color: grey;
}

.toast {
position: absolute;
z-index: 1000;
left: 50%;
top: 56px;
transform: translate(-50%, -50%);
padding: 5px 20px;
border: 1px solid black;
border-radius: 5px;
background-color: #444;
opacity: 0.8;
font-family: sans-serif;
color: white;
font-weight: 100;
font-size: 14px;
text-align: center;
}

.cqs_module_item {
margin: 0.5em;
display: inline-block;
Expand Down
35 changes: 34 additions & 1 deletion cq_server/static/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ let modules_name = [];
let viewer = null;
let timer = null;
let sse = null;
let toast_timeout = null;


function init_sse() {
sse = new EventSource('events');
sse.addEventListener('loading_model', event => {
const message = "Reloading model: " + event.data;
show_toast(message);
})
sse.addEventListener('file_update', event => {
render(JSON.parse(event.data));
const data = JSON.parse(event.data);
render(data);
hide_toast();
if (data.time_elapsed !== undefined) {
console.log(`${data.module_name}: model reloaded in ${data.time_elapsed.toFixed(2)} seconds`);
}
})
sse.onerror = error => {
if (sse.readyState == 2) {
Expand Down Expand Up @@ -54,6 +64,29 @@ function init_viewer(_options, _modules_name) {
}
}

function show_toast(message, duration = -1) {
if (toast_timeout) {
clearTimeout(toast_timeout);
toast_timeout = null;
}

document.getElementById('cqs_toast_message').innerText = message;
document.getElementById('cqs_toast').style.display = 'block';

if (duration !== -1) {
toast_timeout = setTimeout(hide_toast, duration);
}
}

function hide_toast() {
if (toast_timeout) {
clearTimeout(toast_timeout);
toast_timeout = null;
}

document.getElementById('cqs_toast').style.display = 'none';
}

function show_error() {
document.title = 'error | CadQuery Server';
document.getElementById('cqs_index').style.display = 'none';
Expand Down
4 changes: 4 additions & 0 deletions cq_server/templates/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<body>
<div id="cad_view"></div>

<div class="toast" id="cqs_toast" style="display: none">
<span id="cqs_toast_message"></span>
</div>

<div class="modal error" id="cqs_error" style="display: none">
<h2>Oops! An error occured.</h2>
<p id="cqs_error_message"></p>
Expand Down