Anvil app repository -- overall package structure

Hello,

I am currently working on a web app (using the CLI anvil runtime on my own computer) that requires a fair amount of “background logic” that I don’t necessarily want to strongly couple with the anvil code.

I am currently using a separate python package (i.e. installable with pip install -e .) that houses all this logic (e.g. calling separate APIs, linking other sources of data, etc.); and then using that (i.e. import my-project as mp in the server_code and client_code sub-packages of the anvil app project.

I’m not sure if there’s a better ‘best practise’ way to do this; as it would make more sense to have all the code in one repository; however, I don’t necessarily want to have the bulk of my classes / functions within the same files as when I do things like @anvil.server.callable .... .

I’ve also played around with using uplinks which could connect my other scripts / python project without importing anything as an installed package; but obviously the my-project code needs to be ‘anvil-aware’ by initiating the uplink setup.

I was also thinking of simply making the whole anvil app repository (i.e. the root directory of the web app that you get when cloning from the browser IDE) a python package itself; i.e. adding a setup.py and doing pip install -e ., which would allow me to add a sub-package within the same repository and still keep things separate by still continuing to use import my-project.

Has anyone had experience with these options of turning their web app repository into a python package with setup.py? Any other ideas?

Cheers!

Odds are, it’s already a package. My local copy (via git) of my Anvil app is already structured as a package, by default, including an __init__.py. Many of my forms are structured as packages, too. And you can explicitly create packages in the IDE, so they’re definitely supported.

In fact, I’ve had good success in embedding (selected) pure-Python packages, verbatim, within my app, under the folder client_code. (For server-side code, I’d put them in server_code.) Placed there, I don’t need to install them. I import them, just like any other packages created within the app.

Edit
Of course, that’s my local copy, but I suspect that the in-server copy would be structured identically. You can confirm that by inspection. Does the root have an __init__.py?

Hi @cam,

Is your code (currently) specific to your Anvil app, or is it meant to be effectively a third-party library used by your Anvil app and by other code?

Because if it’s specific to your app, what I’d recommend is implementing your logic as a package in client_code (or server_code if you don’t need to load it in the browser). Then you can import those Python packages from your ordinary Anvil code, and keep the Anvil-specific logic there. That’s probably the easiest way to do it - the alternative involves a fair bit of messing around with pip.

1 Like

Thanks for the feedback. At the moment, I think it’s more appropriate to be a 3rd party library as there is nothing in the code that is web-specific and serves more as utils. So I have added it as a python package with pip to the virtual environment that I’m serving the web app on.

It also allows me to import directly using the package name, as opposed to relative imports (which I always find quite confusing!)