Home / Class/ ScriptInfo Class — flask Architecture

ScriptInfo Class — flask Architecture

Architecture documentation for the ScriptInfo class in cli.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  9fb0ba66_32bf_1412_77b1_174dae79c4b2["ScriptInfo"]
  a96499c3_f8a9_e782_f156_1c1ee4a86c69["cli.py"]
  9fb0ba66_32bf_1412_77b1_174dae79c4b2 -->|defined in| a96499c3_f8a9_e782_f156_1c1ee4a86c69
  e4e08be1_e9e6_7b53_481b_3e13fc8c8378["__init__()"]
  9fb0ba66_32bf_1412_77b1_174dae79c4b2 -->|method| e4e08be1_e9e6_7b53_481b_3e13fc8c8378
  6cabef6b_f1ce_76af_5916_c4e9a439548c["load_app()"]
  9fb0ba66_32bf_1412_77b1_174dae79c4b2 -->|method| 6cabef6b_f1ce_76af_5916_c4e9a439548c

Relationship Graph

Source Code

src/flask/cli.py lines 293–372

class ScriptInfo:
    """Helper object to deal with Flask applications.  This is usually not
    necessary to interface with as it's used internally in the dispatching
    to click.  In future versions of Flask this object will most likely play
    a bigger role.  Typically it's created automatically by the
    :class:`FlaskGroup` but you can also manually create it and pass it
    onwards as click object.

    .. versionchanged:: 3.1
        Added the ``load_dotenv_defaults`` parameter and attribute.
    """

    def __init__(
        self,
        app_import_path: str | None = None,
        create_app: t.Callable[..., Flask] | None = None,
        set_debug_flag: bool = True,
        load_dotenv_defaults: bool = True,
    ) -> None:
        #: Optionally the import path for the Flask application.
        self.app_import_path = app_import_path
        #: Optionally a function that is passed the script info to create
        #: the instance of the application.
        self.create_app = create_app
        #: A dictionary with arbitrary data that can be associated with
        #: this script info.
        self.data: dict[t.Any, t.Any] = {}
        self.set_debug_flag = set_debug_flag

        self.load_dotenv_defaults = get_load_dotenv(load_dotenv_defaults)
        """Whether default ``.flaskenv`` and ``.env`` files should be loaded.

        ``ScriptInfo`` doesn't load anything, this is for reference when doing
        the load elsewhere during processing.

        .. versionadded:: 3.1
        """

        self._loaded_app: Flask | None = None

    def load_app(self) -> Flask:
        """Loads the Flask app (if not yet loaded) and returns it.  Calling
        this multiple times will just result in the already loaded app to
        be returned.
        """
        if self._loaded_app is not None:
            return self._loaded_app
        app: Flask | None = None
        if self.create_app is not None:
            app = self.create_app()
        else:
            if self.app_import_path:
                path, name = (
                    re.split(r":(?![\\/])", self.app_import_path, maxsplit=1) + [None]
                )[:2]
                import_name = prepare_import(path)
                app = locate_app(import_name, name)
            else:
                for path in ("wsgi.py", "app.py"):
                    import_name = prepare_import(path)
                    app = locate_app(import_name, None, raise_if_not_found=False)

                    if app is not None:
                        break

        if app is None:
            raise NoAppException(
                "Could not locate a Flask application. Use the"
                " 'flask --app' option, 'FLASK_APP' environment"
                " variable, or a 'wsgi.py' or 'app.py' file in the"
                " current directory."
            )

        if self.set_debug_flag:
            # Update the app's debug flag through the descriptor so that
            # other values repopulate as well.
            app.debug = get_debug_flag()

        self._loaded_app = app
        return app

Defined In

Frequently Asked Questions

What is the ScriptInfo class?
ScriptInfo is a class in the flask codebase, defined in src/flask/cli.py.
Where is ScriptInfo defined?
ScriptInfo is defined in src/flask/cli.py at line 293.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free