Is there any way to reload a specific jupyter notebook cell on a click of anvil button?
Is there any way to refresh all/specific jupyter notebook cells, by refreshing anvil my web page? or somehow anvil button can do it for me? what functionality will be required at the back-end part? I’m stuck here.
I successfully got the jupyter notebook cell refreshed on the click of anvil button but i realized that anvil text boxes,file loader, image field, labels are still filled with my data (which i also want to be cleared). I am unable to find anything regarding refreshing anvil page on click of button. So i had to manually set every thing to None like this:
def new_model_click(self, **event_args):
anvil.server.call('clear')
self.class0.text = None
self.class1.text = None
self.epoch1.text = None
self.epoch2.text = None
self.input.text = None
self.file_loader_2.clear()
self.d.text = None
#self.result_lbl2 = None
#self.size2 = None
#self.image_2.source = None
Everything’s working fine except the last three lines of code which i commented out.
“result_lbl2” and “size2” are labels which i want to set as it is when the anvil page loads every time.
“image_2” is an image field where i have set an image of my choice (but later it changes every time when i upload a new image, which is working perfectly fine). Now, on the button click, i want this image to set back to the default image.
How can i run these last three lines to work as i mentioned above?
Here are some screenshots:
Is it that you want the label object itself to be removed? Or just the text
showing in the label? See Label Properties for the code-accessible aspects of a label.
No sorry i was getting some error so i tried to remove the error randomly
Actually when i load this page, “result_lbl2” is set to “PREDICTED RESULT”. And then i am replacing my output value with this label using some functions (which is working perfectly). Now i want to set it back to PREDICTED RESULT whenever i click the button “TRAIN NEW MODEL”. i.e. I want to start the page all over again or simply RELOAD THE PAGE so that all the previously entered values now set to None.
[EDIT]: well these simple lines worked for me.
self.result_lbl2.text = 'PREDICTED RESULT'
self.size2.text = 'IMAGE SIZE'
But i am still stuck on this image part. How can i reset it to an image of my choice?
I tried this but no image appeared:
my_image = r"C:\Users\user\Desktop\img1.jpg"
self.image_2.source = URLMedia(my_image)
All of this manual coding is really upsetting me. There should be a way to refresh a page within an anvil page through a button. But unfortunately i couldn’t find any way to do this.
Regarding “refresh a page”:
Every application page is a user- (developer-) defined structure. So what “refresh” means – or whether it makes any sense at all – is equally situation-dependent. Only the developer understands their specific situation.
Even, then, as understanding and/or requirements change, “refresh” (or any other function of the application) can change, too.
Regarding image locations:
The r
is truly handy for representing DOS/Windows paths. Unfortunately, code running in the browser is prohibited (by browser security rules) from accessing local files, except by specific permission from the user.
But you can copy images into the application itself. This was intended for exactly this situation. See Adding your own asset files.
Thanks!! the following code worked!
my_image = '_/theme/img1.jpg'
self.image_2.source = URLMedia(my_image)
Actually i’ve alot of things going on in my app that’s why i was being specific to my problem. In my app, the user can train a model by providing some inputs. After getting the inputs from user, my app trains the model and returns the “uploaded image” in image field (which was earlier uploaded by user as input), “predicted result” (a label) and “image size” (a label).
After training a model, a user may want to train another one. For this purpose i want to “refresh the page” i.e. removal of the provided inputs from the input fields in the form and start it all over again.
By “refresh” i mean to set the labels as “predicted result” and “image size” again. And set the “image” back to the one which i uploaded manually (whenever a user visits the page, this image will be shown every time. It will change only when the user provides some inputs to train a model).
Try just doing
get_open_form().__init__()
This kind of thing works in lots of python cases eg for lists
>>> x=[1,2,3]
>>> x.__init__()
>>> x
[]
If you had any required properties you can also pass these to the __init__
method
It will depend on how exactly you’ve written your code. But if you’ve done most of the setup in the init method then it might just work.
Oh i got your point i.e. calling __init__
function within another function. That’s a good suggestion.
I tried the following code as you mentioned:
get_open_form().__init__()
and it worked but lead me to home component. I want to stay on the same component which i named t_component
. So i tried the following code:
get_open_form().t_component.__init__()
and got this error:
AttributeError: 'home' object has no attribute 't_component' at [t_component, line 129](javascript:void(0))
I tried to follow this post: Calling a function from within a Template form
But the component names are confusing me. I tried a couple of things but got errors.
You can have a look:
https://anvil.works/build#clone:PWGKX63L7AD4OZVF=EMDLVKG3YBCB7ZW4NNEWX7ZF
whenever i click this TRAIN NEW MODEL button, i want to stay on the same page but all the textboxes should clear up.
FOR ANYONE WHO NEEDS IT.
The following code will work for clearing output from juputer cell on click of anvil button.
Anvil code:
def new_model_click(self, **event_args):
anvil.server.call('clear')
Jupyter notebook code:
from IPython.display import clear_output
@anvil.server.callable
def clear():
clear_output()
has that form been cached or stored somewhere? You just need to know where it lives.
Once you know how to access the t_component form instance you can try calling its init method
(I’d just assumed it was the open form but it’s the form inside the content panel).