summaryrefslogtreecommitdiff
path: root/searx
diff options
context:
space:
mode:
Diffstat (limited to 'searx')
-rw-r--r--searx/result_types/__init__.py4
-rw-r--r--searx/result_types/keyvalue.py49
-rw-r--r--searx/templates/simple/result_templates/keyvalue.html21
3 files changed, 73 insertions, 1 deletions
diff --git a/searx/result_types/__init__.py b/searx/result_types/__init__.py
index 9af16b9b5..e9a115ec3 100644
--- a/searx/result_types/__init__.py
+++ b/searx/result_types/__init__.py
@@ -13,7 +13,7 @@
from __future__ import annotations
-__all__ = ["Result", "MainResult", "EngineResults", "AnswerSet", "Answer", "Translations"]
+__all__ = ["Result", "MainResult", "KeyValue", "EngineResults", "AnswerSet", "Answer", "Translations"]
import abc
@@ -21,6 +21,7 @@ from searx import enginelib
from ._base import Result, MainResult, LegacyResult
from .answer import AnswerSet, Answer, Translations
+from .keyvalue import KeyValue
class ResultList(list, abc.ABC):
@@ -30,6 +31,7 @@ class ResultList(list, abc.ABC):
"""The collection of result types (which have already been implemented)."""
Answer = Answer
+ KeyValue = KeyValue
MainResult = MainResult
Result = Result
Translations = Translations
diff --git a/searx/result_types/keyvalue.py b/searx/result_types/keyvalue.py
new file mode 100644
index 000000000..12efccc4b
--- /dev/null
+++ b/searx/result_types/keyvalue.py
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+"""
+Typification of the *keyvalue* results. Results of this type are rendered in
+the :origin:`keyvalue.html <searx/templates/simple/result_templates/keyvalue.html>`
+template.
+
+----
+
+.. autoclass:: KeyValue
+ :members:
+ :show-inheritance:
+
+"""
+# pylint: disable=too-few-public-methods
+
+from __future__ import annotations
+
+__all__ = ["KeyValue"]
+
+import typing
+from collections import OrderedDict
+
+from ._base import MainResult
+
+
+class KeyValue(MainResult, kw_only=True):
+ """Simple table view which maps *key* names (first col) to *values*
+ (second col)."""
+
+ template: str = "keyvalue.html"
+
+ kvmap: dict[str, typing.Any] | OrderedDict[str, typing.Any]
+ """Dictionary with keys and values. To sort keys, use :py:obj:`OrderedDict`."""
+
+ caption: str = ""
+ """Optional caption for this result."""
+
+ key_title: str = ""
+ """Optional title for the *key column*."""
+
+ value_title: str = ""
+ """Optional title for the *value column*."""
+
+ def __hash__(self) -> int:
+ """The KeyValues objects are checked for object identity, even if all
+ fields of two results have the same values, they are different from each
+ other.
+ """
+ return id(self)
diff --git a/searx/templates/simple/result_templates/keyvalue.html b/searx/templates/simple/result_templates/keyvalue.html
new file mode 100644
index 000000000..0495b46c6
--- /dev/null
+++ b/searx/templates/simple/result_templates/keyvalue.html
@@ -0,0 +1,21 @@
+<article class="result result-keyvalue {% if result.category -%}category-{{ result.category }}{%- endif -%}">
+ <table>
+ {%- if result.caption %}<caption>{{ result.caption }}</caption>{%- endif -%}
+ {%- if result.key_title or result.value_title %}
+ <thead>
+ <tr>
+ <th class="col-key" scope="col" >{{result.key_title}}</th>
+ <th class="col-value" scope="col" >{{result.value_title}}</th>
+ </tr>
+ </thead>
+ {%- endif -%}
+ {%- for key, value in result.kvmap.items() -%}
+ <tr class="{{ loop.cycle('odd', 'even') }}">
+ <th class="col-key" scope="row">{{ key }}</th>{{- '' -}}
+ <td class="col-value">{{ value }}</td>{{- '' -}}
+ </tr>
+ {%- endfor -%}
+ </table>{{- '' -}}
+ <div class="engines">{% for engine in result.engines %}<span>{{ engine }}</span>{% endfor %}</div>{{- '' -}}
+ <div class="break"></div>{{- '' -}}
+</article>