Importing git repository to server code

Hi

I would like to add an existing git repository I work on to my server code.

Background:
There is some python code I want to run on the server side of my Anvil website. This code is managed on my local computer. It would be great if I could git clone this repository of mine on to my anvil server.

I would then want to periodically update the server code (using the git pull command)

There seems to be no mentioning of cloning into the server code in the Anvil documentation (Only how to clone the existing code to a remote device)
How do I do this?

From a git perspective, you would:

  1. Clone the Anvil app to your local machine (or pull from Anvil to update it)
  2. Make changes to the repo on your local machine (adding files, etc)
  3. Commit and push those changes back to Anvil

Depending on what you’re doing in #2, you may or may not have any luck. I’m sure there are things you could do that would break the Anvil app, just as there are things you could do that would not get you what you wanted. But at least you could always revert to the previous commit and push to Anvil to get out of trouble.

If you’re talking about embedding another repository inside the Anvil app’s repository, I’m sure there are ways of doing that in git as well. They’d be done in the #2 step above. That’s beyond my git knowledge, though.

You can try with the Python 3.10 beta that allows to install any package you like.

I need a recent bug fix to fpdf2, so I am experimenting with this right now:

In this question there are considerations that may be helpful.

EDIT
I don’t know what happens (because I don’t own that repository and I haven’t tested with a repository I own) when the master has a new commit, but has no new version.

I know Anvil caches the environments, so it doesn’t need to rebuild them every time you use them, but I don’t know any details about the caching.

I assume the key for the caching is requirements.txt, so any changes to requirements.txt will trigger the creation (or retrieval from cache) of the new environment. If this is the case, then using the master branch may cause the environment to be built with whatever master is at the time the environment is built and never be updated. And if this is the case, I wouldn’t know how to trigger a new update when master changes.

So, if you want to work with your own repository, you may need to create a new package version, then update requirements.txt in Anvil every time you change your repository. Maybe.

1 Like

That is correct!

You can also specify a particular commit in your requirements – eg git+https://github.com/owner/repo@41b95ec – which will cause a rebuild with the exact version you specify!

2 Likes

So you could add/change a comment, and it would rebuild?

If the text in requirements.txt doesn’t change, it doesn’t rebuild, and requirements.txt only changes when the user goes through the app settings, so there is never going to be an automatic rebuild.
Good!

If the user goes through the settings and changes the commit id or branch name or version, then the rebuild is triggered.

This means that using the branch name in requirements.txt, installs the branch the way it was at installation time and doesn’t automatically update when the branch is updated.

This behavior may seem to create inconsistencies, but it’s identical to what happens with custom environments managed locally on our computers.

Hi

Thanks for all the replies
Some things you mentioned don’t make sense to me (I would love you to dumb it down a little bit)

When you say package, do you mean my repository? My repository is basically a folder with a bunch of python scripts in it

I have a repository that I edit–>commit–>push periodically. What do you mean by “has no version”?

I currently don’t have a requirements.txt file in my Anvil app or my personal repository. Do you recommend I add one with the line you specified?

To conclude:
I feel a lot of important information has been detailed in this post but I am still not able to understand how I can add my own repository to my Anvil app and push changes when made.

This post will provide the missing context for what Stefano was describing: 📢 New Beta: Install Any Python Package Instantly

Updating would still be manual process of updating requirements.txt to point to a new commit.

OK
I’m currently giving up on being able to update my repository by git pull into my Anvil app
It seems that any module I add to my server code I cannot run

Trying to simplify my issue:
Lets say I have added a .py script of my own to my server code. The name of the file is MeshCreator.py

When attempting to run the code I receive the following error:

In the app command line this is the error explanation:
ImportError: cannot import name 'MeshCreator' from 'UITry' (unknown location)

I have tried adding the word MeshCreator to the requirements.txt file you proposed but receive a build error:
image

MeshCreator.py has no official version (It is a basic script I wrote on my own)

Is the MeshCreator.py script what you call a package?
How do I install this package so my anvil app will be able to run it?

Here is my app:

Try adding a module from the ide, then paste the code into it.

If that works, then you can clone it and use git instead of copy/paste.

@stefano.menci It didn’t work…
I also added the @anvil.server.callable before each of the methods to no avail

This is too generic, some more details would help.

Can you share a clone link?

1 Like