Search code examples
gramex

is it possible to assign dynamic value from request-path to `table` attribute for FormHandler gramex-config entry?


ref to FormHandler-param-doc below is the sample gramex-config snippet: dburl: postgresql://$db_name:$db_name@localhost:5432/$db_name

  data_filter:
    pattern: /$YAMLURL/data_filter/(\w+)
    handler: FormHandler
    kwargs:
      url: $dburl
      table: {_0}
      modify: data_filter.by_month_date(data)

Is it possible to assign value dynamically for table attribute from part of request-URL path? for a sample request like: /data_filter/prod_rec_20?S_CODE=20&D_CODE=322&Market_Code=10753&Crop_Code=106 Getting the below error:

Traceback (most recent call last):
  File "c:\programdata\anaconda3\lib\site-packages\gramex\handlers\formhandler.py", line 157, in get
    result[key] = yield val
  File "c:\programdata\anaconda3\lib\site-packages\tornado\gen.py", line 1133, in run
    value = future.result()
  File "c:\programdata\anaconda3\lib\concurrent\futures\_base.py", line 425, in result
    return self.__get_result()
  File "c:\programdata\anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception
  File "c:\programdata\anaconda3\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "c:\programdata\anaconda3\lib\site-packages\gramex\data.py", line 247, in filter
    raise ValueError('No table: or query: specified')
ValueError: No table: or query: specified

Solution

  • Sure. Please see https://learn.gramener.com/guide/formhandler/#formhandler-parameters

    You can specify table: '{_0}'. Then /data_filter/?table=prod_rec_20 would work.

    table: {_0} without the quotes won't work, though. YAML interprets the {} as an object. You need to quote the '{_0}' for this to work. (I tested it, and it's working fine.