Skip to content

Instantly share code, notes, and snippets.

@fmind
Last active January 7, 2022 08:25
Show Gist options
  • Save fmind/c065bf6c70fa37d06789f30c42fd07cd to your computer and use it in GitHub Desktop.
Save fmind/c065bf6c70fa37d06789f30c42fd07cd to your computer and use it in GitHub Desktop.
Snippets for Typer applications.
"""Manage the Command-Line Interface (CLI) of the project."""
# IMPORTS
import typing as T
import typer
# CONFIGS
app = typer.Typer()
# store the app state
state: T.Dict[str, T.Any] = {}
# COMMANDS
@app.callback()
def main():
"""Manage the Command-Line Interface (CLI) of the project."""
# CONDITIONS
if __name__ == "__main__":
app()
"""Manage the Graphical User Interface (GUI) of the project."""
# IMPORTS
import typing as T
import typer
from project import interface # TODO
# CONFIGS
app = typer.Typer()
# store the app state
state: T.Dict[str, T.Any] = {}
# COMMANDS
@app.command("command")
def command():
"""Launch a command of the project."""
launch_arguments = state.get("launch", {})
interface.gui.launch(**launch_arguments)
@app.callback()
def main(
address: str = typer.Option("127.0.0.1", help="IP for running the server."),
port: int = typer.Option(7000, help="Port number for running the server."),
debug: bool = typer.Option(False, help="Start the server in debug mode."),
auth_message: str = typer.Option(None, help="Authentication message."),
enable_queue: bool = typer.Option(True, help="Use it to enable queueing."),
open_browser: bool = typer.Option(False, help="Open browser on launch."),
share_link: bool = typer.Option(False, help="Create a shareable link."),
show_error: bool = typer.Option(False, help="Show error in console."),
credentials: str = typer.Option(
None, envvar="PROJECT_CREDENTIALS", help='Credentials: "User:Pass"'
),
):
"""Initialize common parameters for the commands in this file."""
# provide auth. username/password separate by a colon (:)
auth = credentials.split(":") if credentials else None
# store launch arguments in a global state variable
state["launch"] = {
"auth": auth,
"auth_message": auth_message,
"debug": debug,
"enable_queue": enable_queue,
"inbrowser": open_browser,
"server_name": address,
"server_port": port,
"share": share_link,
"show_error": show_error,
}
# CONDITIONS
if __name__ == "__main__":
app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment