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.
Common files are stored in git which means they can be changed, tested and reviewed on branches.
Example
Suppose you have a utils.py
in the same directory as your notebook. Your notebook code might look like this:
import utils
def predict(inputs):
utils.log_inputs(inputs)
return 5
Before deploying, add utils.py
to common files so it'll be available to your deployment:
mb.add_common_files("utils.py")
Now, when you mb.deploy(...)
, your utils.py
will be included in the deployment's environment.
Adding common files
Common files can be added from your 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, just add your common files to the common
directory at the root of your Modelbit repository.
Using common files
Common files are copied into the deployment when the deployment version is created. 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. When a new deployment is created it'll have utils.py
alongside source.py
:
data/
model.pkl
utils.py
source.py
Common files supports directories. For example, if you want your common files to be copied into your deployments in a lib
directory, put your common files in a sub directory called lib
.
In a notebook, that could look like this:
mb.add_common_files({"path/to/utils.py": "lib/utils.py"})
In git, you'd place your utils.py
in common/lib/utils.py
in your Modelbit repo.
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
and git push
.
Working with common files locally
You can append your Modelbit git repository's common
directory to your Python sys.path
to simulate your deployment's access to common files in your local environment.
import sys
sys.path.append("path/to/modelbit-repo/common")
When updating common files during local development, be sure to mb.add_common_files
or git push
them before deploying new versions of your deployment. Your local changes will only be available to new versions of your deployment if they've been uploaded to Modelbit before the deployment version is created.
See also
More information is available in the API reference for common files: