Home / Function/ add_url_rule() — flask Function Reference

add_url_rule() — flask Function Reference

Architecture documentation for the add_url_rule() function in app.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  4a067460_947c_1e17_83f2_27f565351cdb["add_url_rule()"]
  38f6d4a2_834e_2acd_e1b6_f45c58079ccd["App"]
  4a067460_947c_1e17_83f2_27f565351cdb -->|defined in| 38f6d4a2_834e_2acd_e1b6_f45c58079ccd
  dd1e88a5_022f_b7a5_beb9_d356fce722be["_endpoint_from_view_func()"]
  4a067460_947c_1e17_83f2_27f565351cdb -->|calls| dd1e88a5_022f_b7a5_beb9_d356fce722be
  7889a405_44a8_098c_785b_8fb560b8ae6b["route()"]
  4a067460_947c_1e17_83f2_27f565351cdb -->|calls| 7889a405_44a8_098c_785b_8fb560b8ae6b
  cd459c23_b1c5_45ac_a393_26c2ff268367["get()"]
  4a067460_947c_1e17_83f2_27f565351cdb -->|calls| cd459c23_b1c5_45ac_a393_26c2ff268367
  878f8299_c5bc_f34c_67ae_8bae8fd95fa0["add_url_rule()"]
  4a067460_947c_1e17_83f2_27f565351cdb -->|calls| 878f8299_c5bc_f34c_67ae_8bae8fd95fa0
  style 4a067460_947c_1e17_83f2_27f565351cdb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/sansio/app.py lines 602–658

    def add_url_rule(
        self,
        rule: str,
        endpoint: str | None = None,
        view_func: ft.RouteCallable | None = None,
        provide_automatic_options: bool | None = None,
        **options: t.Any,
    ) -> None:
        if endpoint is None:
            endpoint = _endpoint_from_view_func(view_func)  # type: ignore
        options["endpoint"] = endpoint
        methods = options.pop("methods", None)

        # if the methods are not given and the view_func object knows its
        # methods we can use that instead.  If neither exists, we go with
        # a tuple of only ``GET`` as default.
        if methods is None:
            methods = getattr(view_func, "methods", None) or ("GET",)
        if isinstance(methods, str):
            raise TypeError(
                "Allowed methods must be a list of strings, for"
                ' example: @app.route(..., methods=["POST"])'
            )
        methods = {item.upper() for item in methods}

        # Methods that should always be added
        required_methods: set[str] = set(getattr(view_func, "required_methods", ()))

        # starting with Flask 0.8 the view_func object can disable and
        # force-enable the automatic options handling.
        if provide_automatic_options is None:
            provide_automatic_options = getattr(
                view_func, "provide_automatic_options", None
            )

        if provide_automatic_options is None:
            if "OPTIONS" not in methods and self.config["PROVIDE_AUTOMATIC_OPTIONS"]:
                provide_automatic_options = True
                required_methods.add("OPTIONS")
            else:
                provide_automatic_options = False

        # Add the required methods now.
        methods |= required_methods

        rule_obj = self.url_rule_class(rule, methods=methods, **options)
        rule_obj.provide_automatic_options = provide_automatic_options  # type: ignore[attr-defined]

        self.url_map.add(rule_obj)
        if view_func is not None:
            old_func = self.view_functions.get(endpoint)
            if old_func is not None and old_func != view_func:
                raise AssertionError(
                    "View function mapping is overwriting an existing"
                    f" endpoint function: {endpoint}"
                )
            self.view_functions[endpoint] = view_func

Subdomains

Frequently Asked Questions

What does add_url_rule() do?
add_url_rule() is a function in the flask codebase, defined in src/flask/sansio/app.py.
Where is add_url_rule() defined?
add_url_rule() is defined in src/flask/sansio/app.py at line 602.
What does add_url_rule() call?
add_url_rule() calls 4 function(s): _endpoint_from_view_func, add_url_rule, get, route.

Analyze Your Own Codebase

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

Try Supermodel Free