Is there a way I can move/copy entire forms and modules from project to project?

I see a way to export and import entire projects. But sometimes, one needs to be slightly more selective, and just import a subset. Or copy and paste a form/module to another project.

This would let a project serve as a testing-ground and repository, to supply working parts to other projects.

Is there a way I can do this?

2 Likes

I second this request.
At the moment I cut & paste from my test bed project (first the form, then the code), but it’s less than ideal.

Yes, that’s a decent workaround. Thanks.

Am I missing something here? Why couldn’t you set the testing-ground project as a dependency of your production projects, and import specific forms? The App Dependencies interface seems to imply this will work.

Of course, I’m asking because I have set one project to be dependent on another, where I don’t want to maintain two separate copies of basically identical forms, but I’m getting an error:

TypeError: ā€˜ā€™ object is not callable at …"

when I try to use the form with

import [Project]
import [Project].[Form]
…
get_open_form().content_panel.add_component([Project].[Form](row))

The error occurs on the add_component line, the imports succeed without error.

The only way I have been able to share forms is to expose the shared form in the dependency project as a custom component.

The dependent project then shows it in the widgets section. i then use a wrapper form in the dependent project and add the custom form to it.

Would be great to get some clarification from Anvil on how we do this properly (if that is indeed not the proper way).

Ok, this works for me (excuse the odd naming) :

Expose a project as ā€œsubsubā€ that contains a form ā€œForm1ā€.
In your main project, add the dependency ā€œsubsubā€ and the following code :

import subsub
from subsub import Form1
...
new_form = Form1.Form1()
self.add_component(new_form)

You may be able to simplify that if you’re better at Python than me, but it seems to work.

If I remember correctly, at the time, App Dependencies was not available. It was in alpha or beta. Anvil has been steadily improving over time, and that’s continuing.

A little clarification about how importing works in Python: you can import either a module or a class from that module. These two snippets do the same thing:

import subsub
self.add_component(subsub.Form1.Form1())

from subsub import Form1
self.add_component(Form1.Form1())

You can also import both, but it is not required. For example Anvil does import both, but only to make it easier to access some elements that are used more often:

from tables import app_tables
import tables

The first one is used for things that you use very often, like app_tables.my_table, the latter for other things like tables.order_by(). But you could also use tables.app_tables.my_table

2 Likes

Another way is to:

  1. git clone original
  2. git clone updated
  3. Copy the forms (and directories) of the Forms you need from updated to original (Best to have different form names in original and updated)
  4. git commit the original
  5. git push the original up to anvil.works
1 Like