Hi, If I have an user input in the interface, and want to write a server function which uses that input value, what should I do? I know I am not able to use self.input_box in the server code, what is the best solution to it?
Yes, this is one area very different from desktop applications, where everything runs in a single memory space, on a single machine. In Anvil, code can run in the Browser, or securely, on Anvil’s servers. Only browser-side code can ask questions of the user.
It really depends on the intended workflow. That is, who must provide the information, and under what circumstances.
If you know in advance what data will be needed, then you can ask for it in advance, on the Client (browser) side.
If you don’t know whether/what information will be needed, until the Server-side code makes a decision, then you have to split up the logic:
- The browser asks the server, what question(s) (if any) do I need to ask
- The server replies, with some sort of code, to indicate what the browser needs to do next.
- The browser takes that reply, and asks (or skips) the corresponding question(s).
- If there are any answers that the server side needs, then the browser makes a second server call, to furnish the server with those answers.
I try to avoid this kind of logic when I can. There’s nothing to stop the user from just closing their browser (or losing their connection) in the middle of it all.
In addition to the helpful comment from @p.colbert, Anvil can pass variables from the client (browser) to the server using the decorator @anvil.server.callable
. For example:
In client:
my_text=self.text_box_1.text
# send "my_text" to the server
anvil.server.call("my_server_function", my_text)
In server:
@anvil.server.callable
def my_server_function(my_text):
print(my_text)
.
.
.
Good luck with your development and feel free to ask more questions if anything is unclear.
Hi! This method sends the user input “my_text” to the function “my_server_function”. Is there a way to send that input to the server as a global variable? Which could then be used by all the functions in the server module?
Not unless you have a persistent server:
But you could set the value you need in a Database row and then get that in the other server functions.
if you did have a persistent server, you would use the python global keyword to set the global variable.
Welcome to the forum!
You should probably start a new topic with your particular question, since it’s a different question than the original post. That helps people who find this post looking for an answer to the original question not get confused.
In your post, please give way more details about why you want to do what you think you want to do, since the two sentences I quoted are way too vague to give a reasonable answer to.
You can set the value of the global variable in my_server_function
, but that value will only live as long as my_server_function
is running, unless you do one of the following:
- write it to the database (and retrieve it when you need it)
- place the value in session storage (see Anvil Docs | Sessions and Cookies). Only code called from that same browser session will be able to see it.
- as @duncan_richards12 noted, extend the lifetime of the server program.