Hi, below code generates no output. What am I doing wrong?
def text_box_1_pressed_enter(self, **event_args):
“”“This method is called when the user presses Enter in this text box”""
def plus10(self):
self.no = int(self.text_box_1.text)
result = self.no + 10
return result
Thank you for reply. However, I still cannot work it out. The purpose is to get a number from a text box with event as enter, and use it in a function. for simplicity I have used a simple function of adding 10 to the unput number in the text box. here is the code:
def plus10(self):
self.no = int(self.text_box_1.text)
result = self.no + 10
return result ‘’’
however, when I run the form and enter a number I receive neither output nor an error message.
Please let me know how I should rewrite the above code which helps me to understand how to input a number to functions.
In Python, indentation is critical to the meaning of the code. Since we cannot see your code’s indentation, we can’t be sure exactly what your code is expected to do.
So that we can see your indentation, when you post your code here, please put ``` (NOT ''' !), on a line by itself, both before and after your code. Then we should be able to see what your code actually looks like.
On my keyboard, the ` symbol is to the left of the 1 key in the top row. This is distinct from the ’ symbol, which is next to the Enter key.
Thank you for your time and comprehensive explanation.
The purpose is to input a number in a text box as an input of a function (def) and get an output.
Your assistance is much appreciated.
Here is the code:
def text_box_1_pressed_enter(self, **event_args):
def plus10(self):
self.no = int(self.text_box_1.text)
result = self.no + 10
return result
Thank you Tony. If I want to use the input from text_box in a function in my Jupyter Notebook, what the code would be to define the text_box input as a function (def) argument. Kindly be informed that I have successfully connected the Jupyter Notebook and have no problem there. My question is how to define the text_box value as the function argument (Input).
import anvil.server
anvil.server.connect("YO75E5TWMZJA4PVZ4UBTQ65K-PTWWGKF4PJV4WJ2Y")
@anvil.server.callable
def plus10(self):
self.no = int(self.text_box_1.text)
result = self.no +10
self.text_box_2.text = str(result)
anvil.server.wait_forever()
#And below is the code in anvil form.
def text_box_1_pressed_enter(self, **event_args):
"""This method is called when the user presses Enter in this text box"""
anvil.server.call('plus10', self)```
I get the below error:
AnvilSerializationError: Cannot pass Form1 object to a server function: arguments[0] at [Form1, line 19](javascript:void(0))
The problem here is that you are treating jupyter like a server and then expecting it to modify the client components.
This won’t work.
Anything that interacts with clients components needs to be code in a client form or client module.
You can do the calculation on the server, sending and return simple objects like lists and ints. And then update your client components with the results of those server calls. But the server code can’t do the updating.
The function is in JupyterNB while the input data (in above case a number) is in client side. How I should transfer the input from client side to JupyterNB and transfer back the result to client?
After reading anvil docs again, I changed the code as below. However, I still get the error:
In JupyterNB:
def plus10(self):
result = int(self) + 10
return str(result)
anvil.server.wait_forever()
# in forrm
def text_box_1_pressed_enter(self, **event_args):
"""This method is called when the user presses Enter in this text box"""
result = anvil.server.call('plus10', self)
self.text_box_2.text = result
Error : AnvilSerializationError: Cannot pass Form1 object to a server function: arguments[0]
This is how I finally solved it. Thanks all for your help.
# on server
@anvil.server.callable
def plus10(n):
result = int(n) + 10
return str(result)
anvil.server.wait_forever()
#on client
def text_box_1_pressed_enter(self, **event_args):
"""This method is called when the user presses Enter in this text box"""
result = anvil.server.call('plus10', self.text_box_1.text)
self.text_box_2.text = result