-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add plotly run command #3329
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
Open
T4rk1n
wants to merge
6
commits into
dev
Choose a base branch
from
run-cli
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add plotly run command #3329
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
db1dc8d
Add plotly run command
T4rk1n 2fed18b
infer app instance from type
T4rk1n 9ea2418
store_true instead of BooleanOptionalAction
T4rk1n 0a6cfd1
initialize app_instance for check
T4rk1n 9fccb1d
Add test for plotly run cli
T4rk1n 70e9187
Use cache-dependency-path
T4rk1n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._cli import cli | ||
|
||
cli() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import argparse | ||
import importlib | ||
import sys | ||
from typing import Any, Dict | ||
|
||
from dash import Dash | ||
|
||
|
||
def load_app(app_path: str) -> Dash: | ||
""" | ||
Load a Dash app instance from a string like "module:variable". | ||
|
||
:param app_path: The import path to the Dash app instance. | ||
:return: The loaded Dash app instance. | ||
""" | ||
if ":" not in app_path: | ||
raise ValueError( | ||
f"Invalid app path: '{app_path}'. " | ||
'The path must be in the format "module:variable".' | ||
) | ||
|
||
module_str, app_str = app_path.split(":", 1) | ||
|
||
if not module_str or not app_str: | ||
raise ValueError( | ||
f"Invalid app path: '{app_path}'. " | ||
'Both module and variable names are required in "module:variable".' | ||
) | ||
|
||
try: | ||
module = importlib.import_module(module_str) | ||
except ImportError as e: | ||
raise ImportError(f"Could not import module '{module_str}'.") from e | ||
|
||
try: | ||
app_instance = getattr(module, app_str) | ||
except AttributeError as e: | ||
raise AttributeError( | ||
f"Could not find variable '{app_str}' in module '{module_str}'." | ||
) from e | ||
|
||
if not isinstance(app_instance, Dash): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if I run with just the module name, but the module doesn't define a |
||
raise TypeError(f"'{app_path}' did not resolve to a Dash app instance.") | ||
|
||
return app_instance | ||
|
||
|
||
def create_parser() -> argparse.ArgumentParser: | ||
"""Create the argument parser for the Plotly CLI.""" | ||
parser = argparse.ArgumentParser( | ||
description="A command line interface for Plotly Dash." | ||
) | ||
subparsers = parser.add_subparsers(dest="command", required=True) | ||
|
||
# --- `run` command --- | ||
run_parser = subparsers.add_parser( | ||
"run", | ||
help="Run a Dash app.", | ||
description="Run a local development server for a Dash app.", | ||
) | ||
|
||
run_parser.add_argument( | ||
"app", help='The Dash app to run, in the format "module:variable".' | ||
) | ||
|
||
# Server options | ||
run_parser.add_argument( | ||
"--host", | ||
type=str, | ||
help='Host IP used to serve the application (Default: "127.0.0.1").', | ||
) | ||
run_parser.add_argument( | ||
"--port", | ||
"-p", | ||
type=int, | ||
help='Port used to serve the application (Default: "8050").', | ||
) | ||
run_parser.add_argument( | ||
"--proxy", | ||
type=str, | ||
help='Proxy configuration string, e.g., "http://0.0.0.0:8050::https://my.domain.com".', | ||
) | ||
|
||
# Debug flag (supports --debug and --no-debug) | ||
# Note: Requires Python 3.9+ | ||
run_parser.add_argument( | ||
"--debug", | ||
"-d", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable Flask debug mode and dev tools.", | ||
) | ||
|
||
# Dev Tools options | ||
dev_tools_group = run_parser.add_argument_group("dev tools options") | ||
dev_tools_group.add_argument( | ||
"--dev-tools-ui", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable the dev tools UI.", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-props-check", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable component prop validation.", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-serve-dev-bundles", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable serving of dev bundles.", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-hot-reload", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable hot reloading.", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-hot-reload-interval", | ||
type=float, | ||
help="Interval in seconds for hot reload polling (Default: 3).", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-hot-reload-watch-interval", | ||
type=float, | ||
help="Interval in seconds for server-side file watch polling (Default: 0.5).", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-hot-reload-max-retry", | ||
type=int, | ||
help="Max number of failed hot reload requests before failing (Default: 8).", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-silence-routes-logging", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable silencing of Werkzeug's route logging.", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-disable-version-check", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable the Dash version upgrade check.", | ||
) | ||
dev_tools_group.add_argument( | ||
"--dev-tools-prune-errors", | ||
action=argparse.BooleanOptionalAction, | ||
help="Enable/disable pruning of tracebacks to user code only.", | ||
) | ||
|
||
return parser | ||
|
||
|
||
def cli(): | ||
"""The main entry point for the Plotly CLI.""" | ||
sys.path.insert(0, ".") | ||
parser = create_parser() | ||
args = parser.parse_args() | ||
|
||
try: | ||
if args.command == "run": | ||
app = load_app(args.app) | ||
|
||
# Collect arguments to pass to the app.run() method. | ||
# Only include arguments that were actually provided on the CLI | ||
# or have a default value in the parser. | ||
run_options: Dict[str, Any] = { | ||
key: value | ||
for key, value in vars(args).items() | ||
if value is not None and key not in ["command", "app"] | ||
} | ||
|
||
app.run(**run_options) | ||
|
||
except (ValueError, ImportError, AttributeError, TypeError) as e: | ||
print(f"Error: {e}", file=sys.stderr) | ||
sys.exit(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to use that in the CI for some test to avoid regression?