How can I run just the server side in Anvil?

What I’m trying to do:
I try to create a full code in the server side, however I would like to run it stand alone and debug it, with out running the GUI (Form)
Is that possible?
Also can I save files or upload files for the server side to read/write (is it in the asset?)
And can it run 24/7 if I share the app with someone?

You can run code on Anvil’s servers, without having a GUI.

You will need some other way to trigger that code; for example, a Background Task, or an Uplink program, to make your desired anvil.server.call()s. Check the docs and examples for details.

Code running on the Anvil Servers does not have remote debugging enabled. We commonly insert strategically-placed print() calls, when needed, to track down problems. The output from these calls is logged, and can be seen on-screen in a panel in Anvil’s web IDE.

You can upload files, and there is an Assets folder. The Assets folder is for files that don’t change frequently. For data that changes frequently, Anvil’s database is available, directly through Python, and third-party options are available, too (e.g., Google integration).

Running a Server-side program 24/7 is supported, with a paid business plan, whether you share that App or not.

If you’re looking for a quick “Check if this function is working” you can launch a server REPL in the online IDE :

1 Like

You can create a server side module, import it and call its functions from your pc or from a server module, and its behavior will be identical.

If the functions use some Anvil services like datatable or email, then, in order to debug them locally, you will need to first connect to the server with an uplink key, and most of the services will work just like they would if the code was running on the server.

You can even create a module that defines a few server callables, connects to the server with the uplink key and waits for the server to call its functions. Then you can click on a button on a form, the click event calls the server function that runs locally on your computer and (I think, not 100% sure) you can debug it without problems.

Assets are read only.

You have the /tmp folder where you can save whatever you like, but the files may not survive the next server call.

Or you can use the files services to store them in a datatable.

That’s what the server does.

1 Like

If you want to keep a script running 24/7, you will have to launch it as a background task (you will need to have a paid plan otherwise the background task will stop after 30 seconds)

Perhaps sharing some more context can allow us to help you further.

Thanks for the information thus far.
Really appreciate it.
The scenario is as follows:
I would like to be able to send a link to my app to any one I want and would like it to be available without running or preparing anything before hand.
Right now my setup is Google Colab code running and Anvil.
I am thinking maybe to use Google Cloud to be ON all the time, and Anvil will communicate with it.
Is it possible?

What would the recipient do with this link? Without a GUI, pasting the link into their browser would be useless.

The link will be to the Anvil GUI app but the server code will be in the Google Cloud platform.( that will run 24/7)
Is that possible?

An Anvil app has the UI running in the browser and the server side running on the Anvil server (unless you have a very good reason and time and knowledge to install the open source server on your own instance). The app can also communicate with any computer running a script that is connected to the app with an uplink connection, but that script must be running 24/7, so the server can access it whenever it needs it.

The Anvil server is good at running 24/7, that’s what it is designed for, Google Colab not so much (as far as I know).

Sooo… I don’t understand why you are trying to run code 24//7 in Google Colab when Anvil is much better at doing it. Perhaps you have good reasons to do that, but you need to provide some more details. My guess after reading your questions is that you need to forget Google Colab and do all in Anvil. But it’s just a guess because you didn’t provide enough details.

As @stefano.menci pointed out, this could be done by running your “server code” as an Anvil Uplink program on a Python interpreter installed in your instance under Google Cloud. But then you’d have to take care to make sure that your program runs 24/7, and gets re-started whenever it crashes. It can’t respond to browser-side requests if it’s not running. For Uplink programs, available 24/7 == running 24/7.

Code that runs on an Anvil Server doesn’t have that problem. Anvil is happy to start up the server-side code on-demand, so available 24/7 != running 24/7.

That is interesting.

I would like to make sure I understood your last replay :slight_smile:

Using uplink needs a server running somewhere 24/7. (Home computer, Google Colab, Google Cloud, etc…)

Using Anvil for server and GUI doesn’t use uplink; thus doesn’t need to run 24/7. It will “operate” once the user presses on the link for the Anvil app GUI and will engage the server side, as it functions as one unit.

Is that correct?
If so, does the basic plan support that?

Not really.

Your home computer will do automatic updates, go to sleep, have network connection problems, have other interruptions, far from 24/7. Colab wakes up when the user accesses it, I don’t know if it can be configured to stay alert 24/7, I would be surprised if a free plan allowed it.

Your computer and Colab are not web servers.

Anvil on the other hand is a web server, and by definition stays up 24/7 serving, listening and answering. The free plan in Anvil is still a web server and covers you 24/7.

Colab can run an uplink script, which will connect to the Anvil server and wait for requests from Anvil, but I’m guessing it will eventually go to sleep and disconnect until an user will manually restart the uplink script. So no 24/7.

But I’m not an expert in Colab, and there may be options that I don’t know.

Yes, all plans support that

Ok that is great, however it seems that Anvil has a major bug…

If I want to install my custom packages, I can only use Python 3.10 ( why… btw???)
And one of the packages is Earth Engine By Google which uses a command from StringIO lib which doesn’t exist in 3.10 anymore…
So there is no way I can pip install something to fix it.
And python 3.0 is locked for custom installations…
Is it possible to simply install packages to python 3.0???

The free plan is available 24/7, but doesn’t allow custom package installation.

Paid plans allow installation of custom packages in python 3.10 only.

If you want to use older unmaintained packages, you can setup your own server, install your packages in there, create a script that uses uplink to communicate with the Anvil server and expose all the functionalities you need.

What major bug are you talking about?

It’s pretty clear, now, that @odperry started with a specific architecture in mind. This was based on assumptions about Anvil’s own architecture.

Those assumptions may have been reasonable, based on how other web services work, but Anvil works a bit differently.

How about starting with the intended workflow? Then we can fit that to the available architecture and options.

No one has mentioned here that you can create HTTP API server functions in Anvil, and access them with any front end, or with any command line tools that can make HTTP requests. This exposes Anvil server functionality in a way that’s versatile, and which can be accessed universally by just about any common third party language and development infrastructure.

I have a paid basic plan which allows installation.

The bug I am referring to is the one that doesn’t allow you to install packages on the regular python env that most users use, and only on the beta 3.10 version which isn’t supported by many vendors ( such as Google Earth )
To install all the environment on a Google cloud and so on, that is something I would like to avoid, and that was the main reason I started this discussion.

I’d hate to trot out the old “It’s not a bug, It’s a feature” trope, but the way to install and control your own custom packages with anvil is currently, and always was, using anvil-uplink.

The completely Beta and possibly experimental feature of installing packages in anvil is still in beta, and since you are running anything you want from within the anvil servers, it has to be able to run some kind of package installation tool that contains code validation and virus protection, and I am assuming that the best one they could find is only compatible with Python 3.10 and above.

Most people use uplink for this, and installing your own packages is a recent development.

I started with some architecture in mind that uses a dedicated Google Cloud server running and Anvil GUI communicating with it.

However, upon reading your recommendations, I understood that there is a better option, to write my code on the Anvil server side and use it as one unit.
Now I see that it is probably not possible in my case, as Anvil doesn’t allows you to install third-party packages as you see fit, but only in Beta env, which many other vendors don’t support.
Am I missing something?