|
| 1 | +# Copyright 2024 D-Wave |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import argparse |
| 18 | + |
| 19 | +import dash |
| 20 | +import diskcache |
| 21 | +from dash import DiskcacheManager |
| 22 | + |
| 23 | +from demo_configs import APP_TITLE, THEME_COLOR, THEME_COLOR_SECONDARY |
| 24 | +from demo_interface import create_interface |
| 25 | + |
| 26 | +# Essential for initializing callbacks. Do not remove. |
| 27 | +import demo_callbacks |
| 28 | + |
| 29 | +# Fix Dash long callbacks crashing on macOS 10.13+ (also potentially not working |
| 30 | +# on other POSIX systems), caused by https://bugs.python.org/issue33725 |
| 31 | +# (aka "beware of multithreaded process forking"). |
| 32 | +# |
| 33 | +# Note: default start method has already been changed to "spawn" on darwin in |
| 34 | +# the `multiprocessing` library, but its fork, `multiprocess` still hasn't caught up. |
| 35 | +# (see docs: https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods) |
| 36 | +import multiprocess |
| 37 | + |
| 38 | +if multiprocess.get_start_method(allow_none=True) is None: |
| 39 | + multiprocess.set_start_method("spawn") |
| 40 | + |
| 41 | +cache = diskcache.Cache("./cache") |
| 42 | +background_callback_manager = DiskcacheManager(cache) |
| 43 | + |
| 44 | +app = dash.Dash( |
| 45 | + __name__, |
| 46 | + meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}], |
| 47 | + prevent_initial_callbacks="initial_duplicate", |
| 48 | + background_callback_manager=background_callback_manager, |
| 49 | +) |
| 50 | +app.title = APP_TITLE |
| 51 | + |
| 52 | +app.config.suppress_callback_exceptions = True |
| 53 | + |
| 54 | +# Parse debug argument |
| 55 | +parser = argparse.ArgumentParser(description="Dash debug setting.") |
| 56 | +parser.add_argument( |
| 57 | + "--debug", |
| 58 | + action="store_true", |
| 59 | + help="Add argument to see Dash debug menu and get live reload updates while developing.", |
| 60 | +) |
| 61 | + |
| 62 | +args = parser.parse_args() |
| 63 | +DEBUG = args.debug |
| 64 | + |
| 65 | +print(f"\nDebug has been set to: {DEBUG}") |
| 66 | +if not DEBUG: |
| 67 | + print( |
| 68 | + "The app will not show live code updates and the Dash debug menu will be hidden.", |
| 69 | + "If editting code while the app is running, run the app with `python app.py --debug`.\n", |
| 70 | + sep="\n", |
| 71 | + ) |
| 72 | + |
| 73 | +# Generates css file and variable using THEME_COLOR and THEME_COLOR_SECONDARY settings |
| 74 | +css = f"""/* Automatically generated theme settings css file, see app.py */ |
| 75 | +:root {{ |
| 76 | + --theme: {THEME_COLOR}; |
| 77 | + --theme-secondary: {THEME_COLOR_SECONDARY}; |
| 78 | +}} |
| 79 | +""" |
| 80 | +with open("assets/__generated_theme.css", "w") as f: |
| 81 | + f.write(css) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + # Imports the Dash HTML code and sets it in the app. |
| 86 | + # Creates the visual layout and app (see `demo_interface.py`) |
| 87 | + app.layout = create_interface() |
| 88 | + |
| 89 | + # Run the server |
| 90 | + app.run_server(debug=DEBUG) |
0 commit comments