Skip to content

Commit 7390871

Browse files
committed
Cleanup UI for Flask GPUOpen.
1 parent 8087b31 commit 7390871

4 files changed

Lines changed: 195 additions & 119 deletions

File tree

flask/gpuopen/MaterialXGPUOpenApp.py

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
#import MaterialX as mx
1919

2020
class MaterialXFlaskApp:
21+
'''
22+
@brief Base Flask application class for MaterialX GPUOpen server interactions.
23+
'''
2124
def __init__(self, home):
25+
'''
26+
Initialize the Flask application and SocketIO.
27+
@param home The home page template to render.
28+
'''
2229
self.home = home
2330

2431
# Initialize Flask and SocketIO
@@ -31,9 +38,9 @@ def __init__(self, home):
3138
self._register_socket_events()
3239

3340
def _register_routes(self):
34-
"""
41+
'''
3542
Register HTTP routes.
36-
"""
43+
'''
3744
@self.app.route('/')
3845
def home():
3946
"""
@@ -49,17 +56,20 @@ def _setup_event_handler_map(self):
4956
raise NotImplementedError("Subclasses must implement _setup_event_handler_map")
5057

5158
def _register_socket_events(self):
52-
"""
59+
'''
5360
Register SocketIO events.
54-
"""
61+
'''
5562
# Dynamically register event handlers
5663
for event_name, handler in self.event_handlers.items():
5764
self.socketio.on_event(event_name, handler)
5865

5966
def run(self, host, port, debug=True):
60-
"""
67+
'''
6168
Run the Flask server with SocketIO.
62-
"""
69+
@param host The host address to run the server on.
70+
@param port The port to run the server on.
71+
@param debug Whether to run the server in debug mode.
72+
'''
6373
self.socketio.run(self.app, host, port, debug=debug)
6474

6575

@@ -69,9 +79,10 @@ class MaterialXGPUOpenApp(MaterialXFlaskApp):
6979
and extracting of materials by regular expression.
7080
'''
7181
def __init__(self, homePage):
72-
"""
82+
'''
7383
Initialize the Flask application and the MaterialX loader.
74-
"""
84+
@param homePage The home page template to render.
85+
'''
7586
super().__init__(homePage)
7687

7788
# Material loader and associated attributes
@@ -81,16 +92,26 @@ def __init__(self, homePage):
8192
self.material_count = 0
8293

8394
def _emit_status_message(self, message):
84-
"""
85-
Emit a status message to the client.
86-
"""
95+
'''
96+
@brief Emit a status message to the client. The message emitted is of the form:
97+
{ 'message': 'message string' }
98+
99+
@param message The status message to emit.
100+
'''
87101
emit('materialx_status', { 'message': message }, broadcast=True)
88102
print('Python:', message)
89103

90104
def handle_download_materialx(self, data):
91-
"""
92-
Handle the 'download_materialx' event, initialize the loader, and send materials data to the client.
93-
"""
105+
'''
106+
@brief Handle the 'download_materialx' event, initialize the loader, and send materials data to the client.
107+
Data is of the form:
108+
{ 'materialCount': int,
109+
'materialNames': list of strings,
110+
'materialsList': list of material data in JSON format
111+
}
112+
@param data The data received from the client (not used in this handler).
113+
114+
'''
94115
status_message = f'Downloaded materials...'
95116
self._emit_status_message(status_message)
96117

@@ -116,9 +137,11 @@ def handle_download_materialx(self, data):
116137
}, broadcast=True)
117138

118139
def handle_extract_material(self, data):
119-
"""
120-
Handle the 'extract_material' event, extract material data, and send it back to the client.
121-
"""
140+
'''
141+
@brief Handle the 'extract_material' event, extract material data, and send it back to the client.
142+
@param data The data received from the client, expected to contain:
143+
{ 'expression': string, 'update_materialx': bool }
144+
'''
122145
if self.loader is None:
123146
self._emit_status_message('Loader is not initialized. Download materials first.')
124147
return
@@ -175,20 +198,27 @@ def handle_extract_material(self, data):
175198
emit('materialx_extracted', {'extractedData': return_list}, broadcast=True)
176199

177200
def _setup_event_handler_map(self):
178-
"""
201+
'''
179202
Set up dictionary of mapping event names to their handlers
180-
"""
203+
'''
181204
self.event_handlers = {
182205
'download_materialx': self.handle_download_materialx,
183206
'extract_material': self.handle_extract_material,
184207
}
185208

186209
# Main entry point
187210
def main():
211+
'''
212+
@brief Main entry point for the application. Parses command-line arguments and starts the Flask server.
213+
@detail Argument parameters are:
214+
-h/--host: Host address to run the server on (default: 127.0.0.1)
215+
-po/--port: Port to run the server on (default: 8080)
216+
-ho/--home: Home page template (default: MaterialXGPUOpenApp.html)
217+
'''
188218
parser = argparse.ArgumentParser(description="GPUOpen MaterialX Application")
189-
parser.add_argument('--host', type=str, default='127.0.0.1', help="Host address to run the server on (default: 127.0.0.1)")
190-
parser.add_argument('--port', type=int, default=8080, help="Port to run the server on (default: 8080)")
191-
parser.add_argument('--home', type=str, default='MaterialXGPUOpenApp.html', help="Home page.")
219+
parser.add_argument('-hs', '--host', type=str, default='127.0.0.1', help="Host address to run the server on (default: 127.0.0.1)")
220+
parser.add_argument('-p','--port', type=int, default=8080, help="Port to run the server on (default: 8080)")
221+
parser.add_argument('-ho', '--home', type=str, default='MaterialXGPUOpenApp.html', help="Home page.")
192222

193223
args = parser.parse_args()
194224

flask/gpuopen/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies = [
2323
"flask>=2.3.2",
2424
"flask-socketio>=5.3.4",
2525
"argparse>=1.4.0",
26-
"materialxMaterials==1.39.5",
26+
"materialxMaterials>=1.39.5",
2727
]
2828

2929
[project.scripts]

flask/gpuopen/static/js/MaterialXGPUOpenClient.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ export class MaterialX_GPUOpen_Client extends WebSocketClient
5555

5656
handleMaterialXDownLoad(data)
5757
{
58+
const downloadSpinner = document.getElementById('download_spinner');
59+
const downloadStatus = document.getElementById('download_status');
60+
downloadSpinner.classList.add('d-none'); // Hide spinner
61+
downloadStatus.innerText = 'Download Materials';
62+
5863
console.log('WEB: materialx downloaded event:', data);
5964
this.materialCount = data.materialCount;
6065
this.materialsList = data.materialsList;
@@ -87,6 +92,11 @@ export class MaterialX_GPUOpen_Client extends WebSocketClient
8792

8893
handleMaterialXExtract(data)
8994
{
95+
const extractSpinner = document.getElementById('extract_spinner');
96+
extractSpinner.classList.add('d-none'); // Hide spinner
97+
const extractStatus = document.getElementById('extract_status');
98+
extractStatus.innerText = 'Extract';
99+
90100
console.log('WEB: materialx extracted event:', data.extractedData);
91101
const extractedData = data.extractedData[0];
92102
const title = extractedData.title;
@@ -126,14 +136,15 @@ export class MaterialX_GPUOpen_Client extends WebSocketClient
126136

127137
// Create a container for the image and label
128138
const imageContainer = document.createElement('div');
129-
imageContainer.style.display = 'inline-block';
130-
imageContainer.style.margin = '10px';
131-
imageContainer.style.textAlign = 'center'; // Center the label under the image
139+
imageContainer.classList.add('col-sm');
140+
//imageContainer.style.display = 'inline-block';
141+
//imageContainer.style.margin = '10px';
142+
//imageContainer.style.textAlign = 'center'; // Center the label under the image
132143

133144
// Create the image element
134145
const img = document.createElement('img');
135146
img.src = url;
136-
img.width = 200;
147+
img.style.width = "256px";
137148
img.alt = key;
138149

139150
// Add the key as a tooltip
@@ -164,18 +175,25 @@ export class MaterialX_GPUOpen_Client extends WebSocketClient
164175
if (preview_url) {
165176
// Create a container for the image and label
166177
const imageContainer = document.createElement('div');
167-
imageContainer.style.display = 'inline-block';
168-
imageContainer.style.margin = '10px';
169-
imageContainer.style.textAlign = 'center'; // Center the label under the image
178+
imageContainer.classList.add('col-sm');
179+
//imageContainer.style.margin = '10px';
180+
//imageContainer.style.textAlign = 'center'; // Center the label under the image
170181

171182
// Create the image element
172183
const img = document.createElement('img');
173184
img.src = preview_url;
174-
img.width = 200;
185+
img.style.width = "256px";
175186
img.alt = "";
176187

177188
// Add the key as a tooltip
178189
img.title = "Preview render";
190+
191+
// Add "url.txt" file to zip where "url" is the preview URL, for reference
192+
if (zip)
193+
{
194+
const urlFile = new File([preview_url], "url.txt", { type: 'text/plain' });
195+
zip.file("url.txt", urlFile);
196+
}
179197

180198
imageContainer.appendChild(img);
181199
imageDOM.appendChild(imageContainer);
@@ -198,6 +216,11 @@ export class MaterialX_GPUOpen_Client extends WebSocketClient
198216
}
199217

200218
extractMaterials() {
219+
const extractSpinner = document.getElementById('extract_spinner');
220+
const extractStatus = document.getElementById('extract_status');
221+
extractSpinner.classList.remove('d-none'); // Show spinner
222+
extractStatus.innerText = 'Extracting...';
223+
201224
const materialSelect = document.getElementById('materialSelect');
202225
const update_mtlx = document.getElementById('update_mtlx').checked;
203226
let selectedItem = materialSelect.options[materialSelect.selectedIndex].text;
@@ -207,6 +230,10 @@ export class MaterialX_GPUOpen_Client extends WebSocketClient
207230

208231
downloadMaterials() {
209232
console.log("WEB: Emitting download_materialx event");
233+
let downloadSpinner = document.getElementById('download_spinner');
234+
let downloadStatus = document.getElementById('download_status');
235+
downloadSpinner.classList.remove('d-none'); // Show spinner
236+
downloadStatus.innerText = 'Downloading...';
210237
this.emit('download_materialx', {});
211238
}
212239

0 commit comments

Comments
 (0)