Skip to main content

Common files for all deployments

Use common files to share code that all deployments will depend on. Common files are especially useful for utilities that all deployments can use: shared logging and analysis code, or shared data processing helpers. If additional files are only needed for one deployment, use extra files.

Common files are stored in git which means they can be changed, tested and reviewed on branches.

Adding common files to Modelbit

Common files can be added from your Python notebook or git.

In your notebook, run mb.add_common_files(...):

# adds one file, ./utils.py, to common
mb.add_common_files("utils.py")

# adds two files
mb.add_common_files(["utils.py", "helpers.py"])

# adds all files in the local lib directory
mb.add_common_files("lib")

# adds utils.py from a different directory
mb.add_common_files({"path/to/utils.py": "utils.py"})

When using git, add your common files to the common directory at the root of your Modelbit repository.

Using common files

Common files are linked into the deployment when the deployment version is created using mb.deploy(...). For deployments created using git instead of the Python API, common files must be added to the deployment with symlinks.

Updating common files does not update existing deployments to prevent changes in common files from breaking past versions. To include the latest versions of your common files in a deployment, redeploy it.

Suppose you've added utils.py to your common files. The next time you run mb.deploy, the new deployment version will have utils.py alongside source.py:

deployments/example_deployment/
data/
model.pkl
utils.py
source.py

In git, after adding your utils.py to common/utils.py in your Modelbit repo, you'd then create a symlink to link utils.py into the deployments of your choosing.

To create a symlink from common/utils.py to deployments/example_deployment/utils/py, run the following command:

cd deployments/example_deployment
ln -s ../../common/utils.py utils.py

Automatic linking of common files

By default, all common files are added to each new deployment when the deployment is created with mb.deploy(...). You can disable this behavior by toggling the the 'Link automatically' setting for individual common files in the web app. Turning off this toggle will skip including it the next time a deployment is created with md.deploy().

In git, you can manage the settings for which common files are linked by updating common/settings.yaml. The settings file can include file paths or path prefixes using asterisks, relative to the common directory.

common/settings.yaml
schemaVersion: 1
skipAutomaticLinking:
- utils.py
- checkpoints/*

In a Python notebook you can tell Modelbit to include common files that would have otherwise been skipped with the common_files parameter of mb.deploy(). In git, of course, you only symlink the common files you want in the deployment.

Managing common files

You can list and remove common files in your notebook:

# delete two common files
mb.delete_common_files(["utils.py", "lib/helpers.py"])

# list all the common files
mb.common_files()

In git, the same can be accomplished with git pull, delete files in common, and git push.

See also

More information is available in the API reference for common files: