Hi,
I’m evaluating Anvil to see it’s viable to use in a commercial product. We’ll be displaying a KPI dashboard for some PLCs. The KPIs will be uploaded to an MySQL DB by the PLCs.
I have a server microservice that obtain the latest values.
The form has code to get update the KPI elements - gauges, bullets & graphs. I want to update these every 10s.
So I first I tried to Python multiprocessing but Skulpt, the Anvil Python to Javascript transpiler does not support it.
I had a little look around and it appears Javascript has worker threads that can execute separate to the main thread. My thinking is to let the KPI 10s refresh to be run in a worker thread so the main Javascript thread can carry out the processing of the main app and not get blocked by the screen refresh task.
How would i go about utilising these ‘worker threads’ in Anvil/Skulpt?
My form code: -
def update_kpi_elements(lis_dic_kpis, obj_form, str_return_list_key):
'''
updates the KPI gauges, bullets, plots etc...
lis_dic_kpis is a list of dictionaries describes the key performance indicator for the gauge/plot = [{str_kpi_type, int_person_id:, str_subject:, str_date_time_of_test:, flo_grade:, str_plot:},]
e.g. [{kpi_type: grade, int_person_id:1, str_subject:Physics, str_date_time_of_test:, flo_grade:, str_plot:Form1.plot_1}, {kpi_type: grade, int_person_id:2, str_subject:Music, str_date_time_of_test:latest, flo_grade:, str_plot:Form1.plot_FazLatest}]
obj_form Anvil form object, the form with the KPI guages
'''
write_log_msg(str_func_name=update_kpi_elements.__name__, str_state='entry')
lis_dic_kpis = lis_dic_kpis
obj_kpis_form = obj_form
str_return_list_key = str_return_list_key
#let's get the kpi_values that need to be displayed
write_log_msg(str_func_name=update_kpi_elements.__name__, str_state='executing', str_type='DEBUG', str_message=f'Client Message - GOING TO SERVER')
lis_dic_kpis = anvil.server.call('return_kpi_values', lis_dic_kpis, str_return_list_key)
write_log_msg(str_func_name=update_kpi_elements.__name__, str_state='executing', str_type='DEBUG', str_message=f'Client Message - RETURNED FROM SERVER')
write_log_msg(str_func_name=update_kpi_elements.__name__, str_state='executing', str_type='DEBUG', str_message=f'lis_dic_kpis=({lis_dic_kpis})')
#lets build the form plots
flo_grade = lis_dic_kpis[0][str_return_list_key]
write_log_msg(str_func_name=update_kpi_elements.__name__, str_state='executing', str_type='DEBUG', str_message=f'Client Message: flo_grade=({flo_grade}), lis_dic_kpis[0][\'flo_grade\']=({lis_dic_kpis[0][\'flo_grade\']})')
obj_gauge = plotly_graph.Figure(plotly_graph.Indicator(mode="gauge+number",
value=flo_grade,
gauge={'axis': {'range': [None, 100]},
'bar': {'color': "black"},
'steps': [{'range': [0, 30], 'color': "red"}, {'range': [80, 100], 'color': "gold"}],
'threshold': {'line': {'color': "black", 'width': 4},
'thickness': 0.75,
'value': flo_grade}},
domain={
'x': [0, 0],
'y': [0, 0]},
title={'text': 'Grade Latest'}))
obj_kpis_form.plot_HaiderLatest.data = obj_gauge
write_log_msg(str_func_name=update_kpi_elements.__name__, str_state='exit')
class Form1(Form1Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
write_log_msg(str_func_name=Form1.__name__, str_state='entry')
# Any code you write here will run before the form opens.
write_log_msg(str_func_name=Form1.__name__, str_state='entry')
lis_dic_kpis = [{'str_kpi_name': 'Form1.plot_1', 'kpi_type': 'grade', 'int_person_id':1, 'str_subject':'Physics', 'str_date_time_of_test':'latest', 'flo_grade':None},
{'str_kpi_name': 'Form1.plot_FazLatest', 'kpi_type': 'grade', 'int_person_id': 2, 'str_subject': 'Music', 'str_date_time_of_test': 'latest', 'flo_grade':None}]
str_return_list_key = 'flo_grade'
update_kpi_elements(lis_dic_kpis=lis_dic_kpis, obj_form=self, str_return_list_key=str_return_list_key)
write_log_msg(str_func_name=Form1.__name__, str_state='exit')
Thanks
Haider