You’re very welcome!
I also just spotted that Typeform provide a Webhooks API which gives you access to new Typeform responses in real-time.
All you need to do is create a simple HTTP API which can process each response as it comes in. One way is to store the JSON response as a simple object in a Data Table. This function would create an endpoint available at: https://<your-app-id>.anvil.app/_/api/new-response
, and store the JSON object in a Data Table called ‘responses’:
@anvil.server.http_endpoint('/new-response')
def display_response():
# store new form response in your data table 'responses' as a json object
app_tables.responses.add_row(response=request.body_json)
Then, using the Typeform Webhook API, you can create a webhook which will call this endpoint each time a new response is received:
@anvil.server.callable
def create_webhook():
anvil.http.request('https://api.typeform.com/forms/<your-form-id>/webhooks/<name-for-your-webhook>',
method="PUT",
headers={
"authorization": "bearer {}".format(anvil.secrets.get_secret('typeform_key')),
},
json=True,
# 'enabled:true' sends new form responses to the webhook immediately
data= {"url": "https://<your-app-id>.anvil.app/_/api/new-response", "enabled":True})
You have a number of options for displaying the response data once it’s stored in your data table. If you want to check for new data periodically and update your UI to show the new data, you could simply add a timer component to your Form. Then, each time the timer ‘ticks’, you could get data from your Data Table and display it in a Data Grid in your UI, like this:
def timer_1_tick(self, **event_args):
"""This method is called Every [interval] seconds. Does not trigger if [interval] is 0."""
response_data = app_tables.responses.search()
# format the JSON data
formatted_data = [{'date' : x['response']['form_response']['landed_at'][0:10],
'a_1' : x['response']['form_response']['answers'][0]['number'],
'a_2' : x['response']['form_response']['answers'][1]['choice']['label'],
'a_3' : x['response']['form_response']['answers'][2]['number']
} for x in response_data]
self.repeating_panel_1.items = formatted_data
Here’s the clone link if you want to see how it all pieces together:
https://anvil.works/build#clone:7YMN3XWWYIAZLEGK=KTNNO4SH6WYCKKO23TNQY7TI
You’ll need to add a Typeform key to app secrets to get it working but if you take the survey here, the data will be added to the data grid in your UI
Let me know if you have any questions!