Linking two apps to the same backend data with uplinks?

What I’m trying to do:
Is it possible to have two apps talking to the same backend?

What I’ve tried and what’s not working:
I have not tried to do this – making plans for a second app for the same code base in the backend

Code Sample:

# this is a formatted code snippet.
# paste your code between ``` 

Clone link:
share a copy of your app

I’m not sure, but you could just as easily check if it works by seeing if connecting 2 uplink keys succeeds in allowing both connections:

anvil.server.connect("<your Server Uplink key>")

Why not just share the data tables between both apps?

Or look at how dependencies work.

Or give us more details, so we can answer better.

I have a large application running on a standalone DigitalOcean box. It works all day long downloading, transcribing, creating articles, accessing chatgpt, agents creating mp3 files, uploading to wordpress sites and more. I have an app that I built with anvil that is not that pretty because I was learning how it works and want rebuild a new one while that old continues to function. I am building a new frontend, but want to start with adding functionality that is needed now, but not available on the first app - using the M3 frontend template. So I would like to access the background script i have running on the remote box as I do with the original app and build the new functionality beginning starting from here in the M3 app. This is much more than data tables. The site creates new content as a command from the app and the app waits for the content to be returned in addition to just accessing DB data also from the site. But- I think the best first step is to try two uplink keys from the one script –

I thought maybe someone in group might have experience doing this. I will report back with my findings – thank you all.

1 Like

Create a second app, add the existing tables from the first app and add the first app as dependency.

Now your second app can run code from the first one and share its tables.

If you want to share Uplinks between apps, then you can use dependencies! If you have an Uplink connected to an app (and the “share” box is ticked), then any app depending on that app can also call your Uplink functions. So you could connect your Uplink to one app and depend on it from both new and old apps!

If you want to use data tables, you will also have to share them between all three apps to avoid surprises (at that point it might make most sense to share a whole Anvil database between your apps, to make sure all table changes are always up to date).

4 Likes

Hi - thank you for the response.

I attempted your first solution and added my first app as a dependency to the new app – “Use Other Apps As Libraries” – I found app-a in the dependency list and selected it. In my new now ‘test’ app I imported the package name provided in the setup in appA – then i did an import My_Original_App to my home form for the test app. After that I attempted to access anything from the Original app and it failed. I printed a dir(My_Original_App) and got
New session: 23/09/2024, 10:01:30

[‘name’, ‘package’, ‘path’]

Session ended: 23/09/2024, 10:01:39

I do not see any examples of doing this procedure that also show any results. Also I may be misunderstanding the procedure.

If i wanted to access something at the client side from the home form of the new app with dependency set to the old app – what would be the code. For example:
Something like below?
import my_old_app as oldapp
sites = oldapp.Home_Form.anvil.server.call(‘get_sites_from_db’)

If you want to access the same backend, you don’t want to be importing things, you just need to call the functions or background tasks like normal. Adding the dependency adds all the server functions to the app’s list.

Okay thank you. – i had a few hoops show up – having to upload a file to create a single file in the files to create the data files object.

I saw an article in the forum articles that said you had to import the module (original app) – so if I want to access a Form method from the dependency app would I need to import that form? I can cross that bridge when I get to it. Thank you – Jack

Not clear here which phrase you are modifying with “from the dependency app”. Is the Form (and its method) from the dependency app, and you are trying to access it within the main app? Or is the Form a main-app Form, and you are trying to access one of its methods from within the dependency?

Generally speaking, in Python, if you want to call an object’s method, you need only have that object, and it could have been passed to you via a function call.

If you want to call a Form’s constructor, that’s a method on the Form class. Now, that class is an object, contained in a module object. So you’d normally get access to it by first importing the module, and asking the module for the class.

But if you’ve been handed an instance of that Form, as its own object, the object knows all of its methods, so you ask that object to invoke its method. No import required.

1 Like