plugins.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. .. _dev plugin:
  2. =======
  3. Plugins
  4. =======
  5. .. sidebar:: Further reading ..
  6. - :ref:`plugins generic`
  7. Plugins can extend or replace functionality of various components of searx.
  8. Example plugin
  9. ==============
  10. .. code:: python
  11. name = 'Example plugin'
  12. description = 'This plugin extends the suggestions with the word "example"'
  13. default_on = False # disabled by default
  14. # attach callback to the post search hook
  15. # request: flask request object
  16. # ctx: the whole local context of the post search hook
  17. def post_search(request, search):
  18. search.result_container.suggestions.add('example')
  19. return True
  20. External plugins
  21. ================
  22. SearXNG supports *external plugins* / there is no need to install one, SearXNG
  23. runs out of the box. But to demonstrate; in the example below we install the
  24. SearXNG plugins from *The Green Web Foundation* `[ref]
  25. <https://www.thegreenwebfoundation.org/news/searching-the-green-web-with-searx/>`__:
  26. .. code:: bash
  27. $ sudo utils/searxng.sh instance cmd bash -c
  28. (searxng-pyenv)$ pip install git+https://github.com/return42/tgwf-searx-plugins
  29. In the :ref:`settings.yml` activate the ``plugins:`` section and add module
  30. ``only_show_green_results`` from ``tgwf-searx-plugins``.
  31. .. code:: yaml
  32. plugins:
  33. ...
  34. - only_show_green_results
  35. ...
  36. Plugin entry points
  37. ===================
  38. Entry points (hooks) define when a plugin runs. Right now only three hooks are
  39. implemented. So feel free to implement a hook if it fits the behaviour of your
  40. plugin. A plugin doesn't need to implement all the hooks.
  41. .. py:function:: pre_search(request, search) -> bool
  42. Runs BEFORE the search request.
  43. `search.result_container` can be changed.
  44. Return a boolean:
  45. * True to continue the search
  46. * False to stop the search
  47. :param flask.request request:
  48. :param searx.search.SearchWithPlugins search:
  49. :return: False to stop the search
  50. :rtype: bool
  51. .. py:function:: post_search(request, search) -> None
  52. Runs AFTER the search request.
  53. :param flask.request request: Flask request.
  54. :param searx.search.SearchWithPlugins search: Context.
  55. .. py:function:: on_result(request, search, result) -> bool
  56. Runs for each result of each engine.
  57. `result` can be changed.
  58. If `result["url"]` is defined, then `result["parsed_url"] = urlparse(result['url'])`
  59. .. warning::
  60. `result["url"]` can be changed, but `result["parsed_url"]` must be updated too.
  61. Return a boolean:
  62. * True to keep the result
  63. * False to remove the result
  64. :param flask.request request:
  65. :param searx.search.SearchWithPlugins search:
  66. :param typing.Dict result: Result, see - :ref:`engine results`
  67. :return: True to keep the result
  68. :rtype: bool