code: def load_facet_configs(request, table_metadata):
      # Given a request and the metadata configuration for a table, return
      # a dictionary of selected facets, their lists of configs and for each
      # config whether it came from the request or the metadata.
      #
      #   return {type: [
      #       {"source": "metadata", "config": config1},
      #       {"source": "request", "config": config2}]}
      facet_configs = {}
      table_metadata = table_metadata or {}
      metadata_facets = table_metadata.get("facets", [])
      for metadata_config in metadata_facets:
          if isinstance(metadata_config, str):
              type = "column"
              metadata_config = {"simple": metadata_config}
          else:
              assert (
                  len(metadata_config.values()) == 1
              ), "Metadata config dicts should be {type: config}"
              type, metadata_config = list(metadata_config.items())[0]
              if isinstance(metadata_config, str):
                  metadata_config = {"simple": metadata_config}
          facet_configs.setdefault(type, []).append(
              {"source": "metadata", "config": metadata_config}
          )
      qs_pairs = urllib.parse.parse_qs(
          request.query_string, keep_blank_values=True
      )
      for key, values in qs_pairs.items():
          if key.startswith("_facet"):
              # Figure out the facet type
              if key == "_facet":
                  type = "column"
              elif key.startswith("_facet_"):
                  type = key[len("_facet_") :]
              for value in values:
                  # The value is the config - either JSON or not
                  if value.startswith("{"):
                      config = json.loads(value)
                  else:
                      config = {"simple": value}
                  facet_configs.setdefault(type, []).append(
                      {"source": "request", "config": config}
                  )
      return facet_configs
  
file_path: datasette/datasette/facets.py
imports: import json
  import urllib
  from datasette import hookimpl
  from datasette.database import QueryInterrupted
  from datasette.utils import escape_sqlite, path_with_added_args, path_with_removed_args, detect_json1, sqlite3
label_desc: Use image node_id 1 for calling a global function with example usage: load_facet_configs(request, table_metadata) and returns: facet_configs