plugins.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. js_dependencies = tuple() # optional, list of static js files
  15. css_dependencies = tuple() # optional, list of static css files
  16. # attach callback to the post search hook
  17. # request: flask request object
  18. # ctx: the whole local context of the post search hook
  19. def post_search(request, search):
  20. search.result_container.suggestions.add('example')
  21. return True
  22. External plugins
  23. ================
  24. External plugins are standard python modules implementing all the requirements of the standard plugins.
  25. Plugins can be enabled by adding them to :ref:`settings.yml`'s ``plugins`` section.
  26. Example external plugin can be found `here <https://github.com/asciimoo/searx_external_plugin_example>`_.
  27. Register your plugin
  28. ====================
  29. To enable your plugin register your plugin in
  30. searx > plugin > __init__.py.
  31. And at the bottom of the file add your plugin like.
  32. ``plugins.register(name_of_python_file)``
  33. Plugin entry points
  34. ===================
  35. Entry points (hooks) define when a plugin runs. Right now only three hooks are
  36. implemented. So feel free to implement a hook if it fits the behaviour of your
  37. plugin. A plugin doesn't need to implement all the hooks.
  38. .. py:function:: pre_search(request, search) -> bool
  39. Runs BEFORE the search request.
  40. `search.result_container` can be changed.
  41. Return a boolean:
  42. * True to continue the search
  43. * False to stop the search
  44. :param flask.request request:
  45. :param searx.search.SearchWithPlugins search:
  46. :return: False to stop the search
  47. :rtype: bool
  48. .. py:function:: post_search(request, search) -> None
  49. Runs AFTER the search request.
  50. :param flask.request request: Flask request.
  51. :param searx.search.SearchWithPlugins search: Context.
  52. .. py:function:: on_result(request, search, result) -> bool
  53. Runs for each result of each engine.
  54. `result` can be changed.
  55. If `result["url"]` is defined, then `result["parsed_url"] = urlparse(result['url'])`
  56. .. warning::
  57. `result["url"]` can be changed, but `result["parsed_url"]` must be updated too.
  58. Return a boolean:
  59. * True to keep the result
  60. * False to remove the result
  61. :param flask.request request:
  62. :param searx.search.SearchWithPlugins search:
  63. :param typing.Dict result: Result, see - :ref:`engine results`
  64. :return: True to keep the result
  65. :rtype: bool