diff --git a/benchmarks/benchmark_all_examples.py b/benchmarks/benchmark_all_examples.py index 676bfe16ba94d53682ff6e52d7b714d8fa1c2d8b..5687ad8a2be4ae02fb2f0b14f2c5873fe89e798e 100644 --- a/benchmarks/benchmark_all_examples.py +++ b/benchmarks/benchmark_all_examples.py @@ -9,6 +9,10 @@ import pkg_resources from benchmarker import Benchmarker from importlib_resources import path +from flatland.utils import graphics_pil + +graphics_pil.unattended_switch = True + for entry in [entry for entry in importlib_resources.contents('examples') if not pkg_resources.resource_isdir('examples', entry) and entry.endswith(".py") diff --git a/benchmarks/profile_all_examples.py b/benchmarks/profile_all_examples.py index f0280f1ea97f0a827814ddaddaabdc657e7dc4af..b41f52a8cb9a9d261dcaf656b7aa4a11e8885374 100644 --- a/benchmarks/profile_all_examples.py +++ b/benchmarks/profile_all_examples.py @@ -8,11 +8,11 @@ import pkg_resources from importlib_resources import path from benchmarks.benchmark_utils import swap_attr +from flatland.utils import graphics_pil def profile(resource, entry): with path(resource, entry) as file_in: - # TODO remove input() from examples print("*****************************************************************") print("Profiling {}".format(entry)) @@ -25,6 +25,8 @@ def profile(resource, entry): cProfile.run('my_func()', sort='time') +graphics_pil.unattended_switch = True + for entry in [entry for entry in importlib_resources.contents('examples') if not pkg_resources.resource_isdir('examples', entry) and entry.endswith(".py") diff --git a/benchmarks/run_all_examples.py b/benchmarks/run_all_examples.py index 8cc940a3f5526b35519448852a3eab0e7fd85d00..972ac68ee862e4e80a342cf4d5133a42b28d3df2 100644 --- a/benchmarks/run_all_examples.py +++ b/benchmarks/run_all_examples.py @@ -7,14 +7,21 @@ import pkg_resources from importlib_resources import path from benchmarks.benchmark_utils import swap_attr +from flatland.utils import graphics_pil + +graphics_pil.unattended_switch = True for entry in [entry for entry in importlib_resources.contents('examples') if not pkg_resources.resource_isdir('examples', entry) and entry.endswith(".py") and '__init__' not in entry + and 'example_basic_elements_test' not in entry and 'demo.py' not in entry ]: with path('examples', entry) as file_in: + print("") + print("") + print("") print("*****************************************************************") print("Running {}".format(entry)) print("*****************************************************************") diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py index 2332ecf3f0ba2e9ae244d8e4c4c3ab057652f195..22a5c5f25185dd3957470cf7c33c8d6fafd7cda1 100644 --- a/flatland/utils/graphics_pil.py +++ b/flatland/utils/graphics_pil.py @@ -33,6 +33,9 @@ from flatland.core.grid.rail_env_grid import RailEnvTransitions # noqa: E402 class PILGL(GraphicsLayer): + # hack: in continuous integration, we run multiple + unattended_switch = False + def __init__(self, width, height, jupyter=False): self.yxBase = (0, 0) self.linewidth = 4 @@ -157,9 +160,14 @@ class PILGL(GraphicsLayer): def open_window(self): assert self.window_open is False, "Window is already open!" - # use tk.Toplevel() instead of tk.Tk() - # https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist - self.window_root = tk.Tk() + if self.unattended_switch: + # use tk.Toplevel() instead of tk.Tk() since we run all examples from the same python script + # https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist + tk.Toplevel() + else: + tk.Tk() + + self.window_root = tk.Toplevel() self.window_root.withdraw() self.window = tk.Toplevel(self.window_root) self.window.title("Flatland") diff --git a/tox.ini b/tox.ini index 3ff9c893f9e62c7106f49534a6dc0babe96ba0e7..a64031d1698f16a349811c45e54e6dd7b0b4c4c4 100644 --- a/tox.ini +++ b/tox.ini @@ -97,7 +97,8 @@ commands = ; run examples from subfolder to ensure that resources are accessed via resources and not via relative paths sh -c 'mkdir -p {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5' ; pipe echo into python since some examples expect input to close the window after the example is run - sh -c 'cd {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5 && python {toxinidir}/benchmarks/run_all_examples.py' +; sh -c 'cd {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5 && python {toxinidir}/benchmarks/run_all_examples.py' + sh -c 'python {toxinidir}/benchmarks/run_all_examples.py' [testenv:notebooks] basepython = python