From 910819ad60b36befdeb003bbd201a33cfd9157f6 Mon Sep 17 00:00:00 2001 From: Hyun Seok Kim Date: Sun, 24 May 2026 01:29:51 -0700 Subject: [PATCH 1/2] Document customizing Scrapy shell variables --- docs/topics/shell.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/topics/shell.rst b/docs/topics/shell.rst index 5b826ed18..4d7c67f15 100644 --- a/docs/topics/shell.rst +++ b/docs/topics/shell.rst @@ -139,6 +139,25 @@ Those objects are: - ``settings`` - the current :ref:`Scrapy settings ` +Customizing shell variables +--------------------------- + +If you want to make project-specific helpers, imports, or objects available +whenever the Scrapy shell starts, you can create a custom shell command and +override its ``update_vars`` method. See :ref:`topics-commands` for more +information about creating custom Scrapy commands. + +For example, if your project defines a custom ``shell`` command, you can update +the shell namespace like this:: + + from scrapy.commands.shell import Command as ShellCommand + + + class Command(ShellCommand): + def update_vars(self, vars): + super().update_vars(vars) + vars["parse_title"] = lambda response: response.css("title::text").get() + Example of shell session ======================== From 2f3bce7c2856fd847919034e216bb61d6ea09a52 Mon Sep 17 00:00:00 2001 From: Olivia Choi Date: Wed, 27 May 2026 18:41:19 -0700 Subject: [PATCH 2/2] Use code-block directive for shell example --- docs/topics/shell.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/topics/shell.rst b/docs/topics/shell.rst index 4d7c67f15..aa05ab4f9 100644 --- a/docs/topics/shell.rst +++ b/docs/topics/shell.rst @@ -148,7 +148,9 @@ override its ``update_vars`` method. See :ref:`topics-commands` for more information about creating custom Scrapy commands. For example, if your project defines a custom ``shell`` command, you can update -the shell namespace like this:: +the shell namespace like this: + +.. code-block:: python from scrapy.commands.shell import Command as ShellCommand