Hello,
I am working on an app that is made of Viktor Pages.
The parametrisation is presented on several pages, so as the results.
Thus, the results are stored in Viktor Storage after doing calculations using a Button.
Still, if I make the mistake of going to the results pages before launching the calculations, even after launching the calculations, the results won’t update as the Parametrization has not changed, even if the files in the Storage have changed.
I tried to make changes in the Parametrization in the calculations code, but it doesn’t make a difference.
Is there a way to update the results page when the parametrization remains the same and the storage changes ?
Many thanks !
Hi!
Not sure if I fully understand your design, but is it possible you have the result you calculate set to be a short view (i.e. have you specified a duration_guess
shorter than 3 seconds)? If so you could try if setting the view to be a slow view results in a design that suits you.
In the method that calculates the view, you can always query the storage to make sure you have the latest result.
Many thanks for your answer
I tried to add a duration guess, but as when I launch a calculation, the Parametrization remains the same the Update Button doesn’t appear.
My result Page is done as follows :
@TableView("Résultats")
def results_optim(self, params, **kwargs):
dict_results = get_data_stored("results")
df = pd.DataFrame(dict_results)
return TableResult(df)
And my storage functions are :
def store_data(dictionary, name_dict):
# Converts the dictionary in a json
json_data = json.dumps(dictionary)
# Stores the dictionary as json file
Storage().set(name_dict, data=File.from_data(json_data), scope="entity")
def get_data_stored(name_dict):
retrieved_data = Storage().get(name_dict, scope="entity")
file_content = retrieved_data.getvalue()
retrieved_dict = json.loads(file_content)
return retrieved_dict
Hi!
Ok so if I understand you request right, you’d like a way to force the triggering of a view? The following is a slightly hacky workaround, but maybe it does what you want? In this example the user can click a setparamsbutton that switches the value of an invisible booleanfield and thereby force the refresh of a view:
import viktor as vkt
from random import randrange
class Parametrization(vkt.Parametrization):
trigger = vkt.SetParamsButton('click me!', method='test')
switch = vkt.BooleanField('switch for view', default=False, visible=False)
class Controller(vkt.Controller):
parametrization = Parametrization
@vkt.TableView("Student Grades")
def table_view(self, params, **kwargs):
data = [
[123, 'John', 6, False],
[234, 'Steve', randrange(10), True],
[345, 'Mike', 7, False],
]
return vkt.TableResult(data)
def test(self, params, **kwargs):
value = True
if params.switch:
value = False
return vkt.SetParamsResult({'switch': value})
Does that do what you need or am I misunderstanding? My uncertainty is because another logical option would be to simply have the “calculation” button you already have on the same page as the view, which you’ve not done so you probably have a good reason for that…?
Oh additionally, by reading another topic I have a slightly better (less clutter) implementation, because you are allowed to return a non-existing field in the SetParamsResult
. That means you don’t have to add the invisble BooleanField
to your parametrization at all, you can just do this:
import viktor as vkt
from random import randrange
class Parametrization(vkt.Parametrization):
trigger = vkt.SetParamsButton('click me!', method='test')
class Controller(vkt.Controller):
parametrization = Parametrization
@vkt.TableView("Student Grades")
def table_view(self, params, **kwargs):
data = [
[123, 'John', 6, False],
[234, 'Steve', randrange(10), True],
[345, 'Mike', 7, False],
]
return vkt.TableResult(data)
def test(self, params, **kwargs):
return vkt.SetParamsResult({'fake field': None})
Many thanks for your quick answer.
My Button was an ActionButton. I changed it to a SetParamsButton that performs the calculations that I want, but also changes an invisible parameter, and it works perfectly!
1 Like