Skip to content

Commit

Permalink
docs: Fix python code highlight in many examples (#3788)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Oct 14, 2024
1 parent 600451e commit bc7a0ce
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Please follow the next guidelines when adding a new example:
:caption: An example of how to use literal includes of external files
.. literalinclude:: /examples/test_thing.py
:language: python
:caption: All includes should have a descriptive caption
Automatically execute examples
Expand Down
9 changes: 9 additions & 0 deletions docs/usage/applications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ of :class:`Controllers <.controller.Controller>`, :class:`Routers <.router.Route
or :class:`Route handlers <.handlers.BaseRouteHandler>`:

.. literalinclude:: /examples/hello_world.py
:language: python
:caption: A simple Hello World Litestar app

The app instance is the root level of the app - it has the base path of ``/`` and all root level
Expand Down Expand Up @@ -49,6 +50,7 @@ For example, let us create a database connection using the async engine from
establish the connection, and another to close it, and then pass them to the :class:`~litestar.app.Litestar` constructor:

.. literalinclude:: /examples/startup_and_shutdown.py
:language: python
:caption: Startup and Shutdown

.. _lifespan-context-managers:
Expand All @@ -61,6 +63,7 @@ In addition to the lifespan hooks, Litestar also supports managing the lifespan
keep a certain context object, such as a connection, around.

.. literalinclude:: /examples/application_hooks/lifespan_manager.py
:language: python
:caption: Handling a database connection

Order of execution
Expand Down Expand Up @@ -116,6 +119,7 @@ Therefore, :paramref:`~.app.Litestar.state` offers an easy way to share contextu
of the application, as seen below:

.. literalinclude:: /examples/application_state/using_application_state.py
:language: python
:caption: Using Application State

.. _Initializing Application State:
Expand All @@ -127,6 +131,7 @@ To seed application state, you can pass a :class:`~.datastructures.state.State`
:paramref:`~.app.Litestar.state` parameter of the Litestar constructor:

.. literalinclude:: /examples/application_state/passing_initial_state.py
:language: python
:caption: Using Application State

.. note:: :class:`~.datastructures.state.State` can be initialized with a :class:`dictionary <dict>`, an instance of
Expand Down Expand Up @@ -166,6 +171,7 @@ To discourage its use, Litestar also offers a builtin :class:`~.datastructures.s
You can use this class to type state and ensure that no mutation of state is allowed:

.. literalinclude:: /examples/application_state/using_immutable_state.py
:language: python
:caption: Using Custom State to ensure immutability

Application Hooks
Expand All @@ -187,6 +193,7 @@ The :paramref:`~litestar.app.Litestar.after_exception` hook takes a
the ``exception`` that occurred and the ASGI ``scope`` of the request or websocket connection.

.. literalinclude:: /examples/application_hooks/after_exception_hook.py
:language: python
:caption: After Exception Hook

.. attention:: This hook is not meant to handle exceptions - it just receives them to allow for side effects.
Expand All @@ -200,6 +207,7 @@ The :paramref:`~litestar.app.Litestar.before_send` hook takes a
sent. The hook receives the message instance and the ASGI ``scope``.

.. literalinclude:: /examples/application_hooks/before_send_hook.py
:language: python
:caption: Before Send Hook

Initialization
Expand All @@ -218,6 +226,7 @@ develop third-party application configuration systems.
called within :paramref:`~litestar.app.Litestar.__init__`, outside of an async context.

.. literalinclude:: /examples/application_hooks/on_app_init.py
:language: python
:caption: Example usage of the ``on_app_init`` hook to modify the application configuration.

.. _layered-architecture:
Expand Down
2 changes: 2 additions & 0 deletions docs/usage/custom-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Litestar supports a mechanism where you provide encoding and decoding hook funct
Here is an example:

.. literalinclude:: /examples/encoding_decoding/custom_type_encoding_decoding.py
:language: python
:caption: Tell Litestar how to encode and decode a custom type

Custom Pydantic types
Expand All @@ -31,4 +32,5 @@ Custom Pydantic types
If you use a custom Pydantic type you can use it directly:

.. literalinclude:: /examples/encoding_decoding/custom_type_pydantic.py
:language: python
:caption: Tell Litestar how to encode and decode a custom Pydantic type
3 changes: 3 additions & 0 deletions docs/usage/routing/handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -576,18 +576,21 @@ However, this approach can get tedious; as an alternative, Litestar accepts a ``
every :ref:`layer <layered-architecture>` of the application, as demonstrated in the following example:
.. literalinclude:: /examples/signature_namespace/domain.py
:language: python
:caption: This module defines our domain type in some central place.
This module defines our controller, note that we do not import ``Model`` into the runtime :term:`namespace`,
nor do we require any directives to control behavior of linters.
.. literalinclude:: /examples/signature_namespace/controller.py
:language: python
:caption: This module defines our controller without importing ``Model`` into the runtime namespace.
Finally, we ensure that our application knows that when it encounters the name "Model" when parsing signatures, that it
should reference our domain ``Model`` type.
.. literalinclude:: /examples/signature_namespace/app.py
:language: python
:caption: Ensuring the application knows how to resolve the ``Model`` type when parsing signatures.
.. tip:: If you want to map your type to a name that is different from its ``__name__`` attribute,
Expand Down
2 changes: 2 additions & 0 deletions docs/usage/routing/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ requests addressed to a given path.
.. dropdown:: Click to see an example of mounting an ASGI app
.. literalinclude:: /examples/routing/mount_custom_app.py
:language: python
:caption: Mounting an ASGI App
The handler function will receive all requests with an url that begins with ``/some/sub-path``, e.g, ``/some/sub-path``,
Expand All @@ -270,6 +271,7 @@ party libraries. The following example is identical in principle to the one abov
.. dropdown:: Click to see an example of mounting a Starlette app
.. literalinclude:: /examples/routing/mounting_starlette_app.py
:language: python
:caption: Mounting a Starlette App
.. admonition:: Why Litestar uses radix based routing
Expand Down
11 changes: 11 additions & 0 deletions docs/usage/routing/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Path :term:`parameters <parameter>` are parameters declared as part of the ``pat
the URL. They are declared using a simple syntax ``{param_name:param_type}`` :

.. literalinclude:: /examples/parameters/path_parameters_1.py
:language: python
:caption: Defining a path parameter in a route handler

In the above there are two components:
Expand Down Expand Up @@ -41,6 +42,7 @@ parameter inside the function declaration is typed with a "higher" type to which
this is fine. For example, consider this:

.. literalinclude:: /examples/parameters/path_parameters_2.py
:language: python
:caption: Coercing path parameters into different types

The :term:`parameter` defined inside the ``path`` :term:`kwarg <argument>` is typed as :class:`int` , because the value
Expand Down Expand Up @@ -68,6 +70,7 @@ If you want to add validation or enhance the OpenAPI documentation generated for
you can do so using the `the parameter function`_:

.. literalinclude:: /examples/parameters/path_parameters_3.py
:language: python
:caption: Adding extra validation and documentation to a path parameter

In the above example, :func:`~.params.Parameter` is used to restrict the value of :paramref:`~.params.Parameter.version`
Expand All @@ -83,6 +86,7 @@ Every :term:`keyword argument <argument>` that is not otherwise specified (for e
:ref:`path parameter <usage/routing/parameters:path parameters>`) will be interpreted as a query parameter.

.. literalinclude:: /examples/parameters/query_params.py
:language: python
:caption: Defining query parameters in a route handler

.. admonition:: Technical details
Expand Down Expand Up @@ -110,6 +114,7 @@ In this example, ``param`` will have the value ``"hello"`` if it is not specifie
If it is passed as a query :term:`parameter` however, it will be overwritten:

.. literalinclude:: /examples/parameters/query_params_default.py
:language: python
:caption: Defining a default value for a query parameter

Optional :term:`parameters <parameter>`
Expand All @@ -124,6 +129,7 @@ If it is given, it has to be a :class:`string <str>`.
If it is not given, it will have a default value of ``None``

.. literalinclude:: /examples/parameters/query_params_optional.py
:language: python
:caption: Defining an optional query parameter

Type coercion
Expand All @@ -133,6 +139,7 @@ It is possible to coerce query :term:`parameters <parameter>` into different typ
A query starts out as a :class:`string <str>`, but its values can be parsed into all kinds of types.

.. literalinclude:: /examples/parameters/query_params_types.py
:language: python
:caption: Coercing query parameters into different types

Alternative names and constraints
Expand All @@ -142,6 +149,7 @@ Sometimes you might want to "remap" query :term:`parameters <parameter>` to allo
than what is being used in the handler function. This can be done by making use of :func:`~.params.Parameter`.

.. literalinclude:: /examples/parameters/query_params_remap.py
:language: python
:caption: Remapping query parameters to different names

Here, we remap from ``snake_case`` in the handler function to ``camelCase`` in the URL.
Expand All @@ -151,6 +159,7 @@ will be used for the value of the ``snake_case`` parameter.
:func:`~.params.Parameter` also allows us to define additional constraints:

.. literalinclude:: /examples/parameters/query_params_constraints.py
:language: python
:caption: Constraints on query parameters

In this case, ``param`` is validated to be an *integer larger than 5*.
Expand All @@ -162,6 +171,7 @@ Unlike *Query* :term:`parameters <parameter>`, *Header* and *Cookie* parameters
declared using `the parameter function`_ , for example:

.. literalinclude:: /examples/parameters/header_and_cookie_parameters.py
:language: python
:caption: Defining header and cookie parameters

As you can see in the above, header parameters are declared using the ``header``
Expand All @@ -176,6 +186,7 @@ As part of Litestar's :ref:`layered architecture <usage/applications:layered arc
of the application:

.. literalinclude:: /examples/parameters/layered_parameters.py
:language: python
:caption: Declaring parameters on different layers of the application

In the above we declare :term:`parameters <parameter>` on the :class:`Litestar app <.app.Litestar>`,
Expand Down
6 changes: 6 additions & 0 deletions docs/usage/security/jwt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ It sends the JWT token using a header - and it expects requests to send the JWT
.. dropdown:: Click to see the code

.. literalinclude:: /examples/security/jwt/using_jwt_auth.py
:language: python
:caption: Using JWT Auth

:class:`JWT Cookie Auth <.security.jwt.JWTCookieAuth>` Backend
Expand All @@ -31,6 +32,7 @@ that instead of using a header for the JWT Token, it uses a cookie.
.. dropdown:: Click to see the code

.. literalinclude:: /examples/security/jwt/using_jwt_cookie_auth.py
:language: python
:caption: Using JWT Cookie Auth

:class:`OAuth2 Bearer <.security.jwt.auth.OAuth2PasswordBearerAuth>` Password Flow
Expand All @@ -43,6 +45,7 @@ OAuth 2.0 Bearer password flows.
.. dropdown:: Click to see the code

.. literalinclude:: /examples/security/jwt/using_oauth2_password_bearer.py
:language: python
:caption: Using OAUTH2 Bearer Password


Expand All @@ -53,6 +56,7 @@ The token class used can be customized with arbitrary fields, by creating a subc
:class:`~.security.jwt.Token`, and specifying it on the backend:

.. literalinclude:: /examples/security/jwt/custom_token_cls.py
:language: python
:caption: Using a custom token


Expand All @@ -79,6 +83,7 @@ a :exc:`NotAuthorizedException` will be raised, returning a response with a


.. literalinclude:: /examples/security/jwt/verify_issuer_audience.py
:language: python
:caption: Verifying issuer and audience


Expand All @@ -93,4 +98,5 @@ dictionary representing the decoded payload, which will then used by


.. literalinclude:: /examples/security/jwt/custom_decode_payload.py
:language: python
:caption: Customizing payload decoding
2 changes: 2 additions & 0 deletions docs/usage/security/secret-datastructures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Secret Parameters
The following example demonstrates how to use :class:`~datastructures.SecretString` to accept a secret value as a parameter in a GET request:

.. literalinclude:: /examples/datastructures/secrets/secret_header.py
:language: python
:caption: Example of using SecretString for a Header Parameter

.. note::
Expand All @@ -35,6 +36,7 @@ This example demonstrates use of a data structure with a :class:`~datastructures
within the HTTP body of a request:

.. literalinclude:: /examples/datastructures/secrets/secret_body.py
:language: python
:caption: Example of using SecretString for a Request Body

Security Considerations
Expand Down
1 change: 1 addition & 0 deletions docs/usage/security/security-backends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ middleware.
.. dropdown:: Click to see an example of using the session auth backend

.. literalinclude:: /examples/security/using_session_auth.py
:language: python
:caption: Using Session Auth

JWT Auth
Expand Down
9 changes: 9 additions & 0 deletions docs/usage/static-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ To serve static files (i.e., serve arbitrary files from a given directory), the
:class:`Router <litestar.router.Router>` to handle this task.

.. literalinclude:: /examples/static_files/full_example.py
:language: python
:caption: Serving static files using :func:`create_static_files_router <litestar.static_files.create_static_files_router>`

In this example, files from the directory ``assets`` will be served on the path
Expand All @@ -24,6 +25,7 @@ Setting :paramref:`~litestar.static_files.create_static_files_router.params.send
them with a ``Content-Disposition: attachment`` instead:

.. literalinclude:: /examples/static_files/send_as_attachment.py
:language: python
:caption: Sending files as attachments using the the
:paramref:`~litestar.static_files.create_static_files_router.params.send_as_attachment` parameter
of :func:`create_static_files_router`
Expand All @@ -40,6 +42,7 @@ This will:
- Attempt to serve ``/404.html`` when a requested file is not found

.. literalinclude:: /examples/static_files/html_mode.py
:language: python
:caption: Serving HTML files using the :paramref:`~litestar.static_files.create_static_files_router.params.html_mode`
parameter of :func:`create_static_files_router`

Expand All @@ -50,6 +53,7 @@ Options available on :class:`~litestar.router.Router` can be passed to directly
:func:`~litestar.static_files.create_static_files_router`:

.. literalinclude:: /examples/static_files/passing_options.py
:language: python
:caption: Passing options to the router generated by
:func:`create_static_files_router`

Expand All @@ -60,6 +64,7 @@ The router class used can be customized with the
:paramref:`~.static_files.create_static_files_router.params.router_class` parameter:

.. literalinclude:: /examples/static_files/custom_router.py
:language: python
:caption: Using a custom router class with
:func:`create_static_files_router`

Expand All @@ -71,6 +76,7 @@ Retrieving paths to static files
under which a specific file will be available:

.. literalinclude:: /examples/static_files/route_reverse.py
:language: python
:caption: Retrieving paths to static files using :meth:`~.app.Litestar.route_reverse`

.. tip:: The ``name`` parameter has to match the ``name`` parameter passed to
Expand All @@ -92,6 +98,7 @@ with support for popular cloud providers available via 3rd party implementations
- Azure Blob Storage via `adlfs <https://github.com/fsspec/adlfs>`_

.. literalinclude:: /examples/static_files/file_system.py
:language: python
:caption: Using a custom file system with
:func:`create_static_files_router`

Expand All @@ -106,8 +113,10 @@ Existing code can be upgraded to :func:`create_static_files_router` by replacing
``route_handlers`` instead of ``static_files_config``:

.. literalinclude:: /examples/static_files/upgrade_from_static_1.py
:language: python
:caption: Using the deprecated :class:`~.static_files.config.StaticFilesConfig`

.. literalinclude:: /examples/static_files/upgrade_from_static_2.py
:language: python
:caption: Upgrading from :class:`~.static_files.config.StaticFilesConfig` to
:func:`create_static_files_router`
2 changes: 2 additions & 0 deletions docs/usage/websockets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ form of JSON.
.. tab-item:: Text

.. literalinclude:: /examples/websockets/receive_str.py
:language: python


.. tab-item:: Bytes

.. literalinclude:: /examples/websockets/receive_bytes.py
:language: python


.. important::
Expand Down

0 comments on commit bc7a0ce

Please sign in to comment.