Cover update_vars() in the shell docs (#7889)

This commit is contained in:
Adrian 2026-08-09 11:25:14 +02:00 committed by GitHub
parent 0b2d220197
commit 9e84112221
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 0 deletions

View File

@ -144,6 +144,32 @@ Those objects are:
- ``settings`` - the current :ref:`Scrapy settings <topics-settings>`
.. _shell-update-vars:
Adding your own objects
-----------------------
To define additional objects, or to run code every time a response is fetched,
write a :ref:`custom project command <topics-commands>` in a module called
``shell``, which overrides the :command:`shell` command, and override its
``update_vars`` method. It is called on start and after every ``fetch``, and it
receives the mapping of variable names to objects:
.. code-block:: python
from scrapy.commands.shell import Command as ShellCommand
class Command(ShellCommand):
def update_vars(self, vars):
from myproject.utils import parse_product
vars["parse_product"] = parse_product
if vars["response"] is not None:
vars["product"] = parse_product(vars["response"])
``response`` is ``None`` when the shell is started without a URL.
Example of shell session
========================