Skip to content

Commit 3def79d

Browse files
committed
feat: Allow user-specified local dir to be served by Tornado
1 parent 4ce9021 commit 3def79d

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

mesa/visualization/ModularVisualization.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ class VisualizationElement:
136136
local_includes: A list of JavaScript and CSS files that are local to
137137
the directory that the server is being run in.
138138
js_code: A JavaScript code string to instantiate the element.
139+
local_dir: A full path to the directory containing the local includes.
140+
If a relative path is given, it is relative to the working
141+
directory where the server is being run. If an absolute path
142+
is given, it is used as-is. Default is the current working
143+
directory.
139144
140145
Methods:
141146
render: Takes a model object, and produces JSON data which can be sent
@@ -147,6 +152,7 @@ class VisualizationElement:
147152
local_includes = []
148153
js_code = ""
149154
render_args = {}
155+
local_dir = ""
150156

151157
def __init__(self):
152158
pass
@@ -291,13 +297,7 @@ def __init__(
291297
tornado.web.StaticFileHandler,
292298
{"path": os.path.dirname(__file__) + "/templates"},
293299
)
294-
local_handler = (
295-
r"/local/(.*)",
296-
tornado.web.StaticFileHandler,
297-
{"path": ""},
298-
)
299-
300-
self.handlers = [page_handler, socket_handler, static_handler, local_handler]
300+
self.handlers = [page_handler, socket_handler, static_handler]
301301

302302
self.settings = {
303303
"debug": True,
@@ -323,11 +323,20 @@ def __init__(
323323
self.package_css_includes.add(include_file)
324324
else:
325325
self.package_js_includes.add(include_file)
326-
for include_file in element.local_includes:
327-
if self._is_stylesheet(include_file):
328-
self.local_css_includes.add(include_file)
329-
else:
330-
self.local_js_includes.add(include_file)
326+
if element.local_includes:
327+
mapped_local_dir = element.__class__.__name__
328+
element_file_handler = (
329+
rf"/local/{mapped_local_dir}/(.*)",
330+
tornado.web.StaticFileHandler,
331+
{"path": element.local_dir},
332+
)
333+
self.handlers.append(element_file_handler)
334+
for include_file in element.local_includes:
335+
include_file_path = f"{mapped_local_dir}/{include_file}"
336+
if self._is_stylesheet(include_file):
337+
self.local_css_includes.add(include_file_path)
338+
else:
339+
self.local_js_includes.add(include_file_path)
331340
self.js_code.append(element.js_code)
332341

333342
# Initializing the model

0 commit comments

Comments
 (0)