-
Install uv (https://docs.astral.sh/uv/). Make sure this is done in/for a Python environment which is referenced in your system $PATH, otherwise you won't be able to call uv in terminal.
-
Create a new local directory and open it in VSCode.
-
Open a terminal window in VSCode (Windows hotkey:
Ctrl + `). -
Run
uv initin the terminal. A bunch of files should be automatically added to the directory, and git is initialised. -
Commit this state as an initial commit in git, and publish to Github.
- The repo will need to be public in order for it to be deployed to Streamlit Community Cloud.
- If you create the repo via Github, initialise it empty (without a README file, for example).
-
Run
hello.pyin terminal using the commanduv run hello.py.- This should create the environment using
uv, including the creation of a.venvsubdirectory (containing a newly created vitual environment for your project) and auv.lockfile in the root directory (which specifies your project's dependencies).
- This should create the environment using
-
Add
streamlitto your virtual environment by runninguv add streamlitin the terminal. Streamlit and its dependencies should all be added to the virtual environment, thepyproject.tomlfile and theuv.lockfile. You can install other dependencies in this way as well (just like usingpip install). -
Check that everthing is working by:
- Modifying
hello.pyto use streamlit, - Activating the virtual environment in terminal by running
.venv\Scripts\activate. - Running
hello.pyusing streamlit in the terminal with the commandstreamlit run hello.py. The streamlit app should launch in you browser and display as expected. - To stop running the app, use
Ctrl + Cin the terminal. This will allow you to continue running other commands in terminal (you will have to close the webpage manually though).
- Modifying
-
Now set up your repo as a Python package to make it easier to organise your code and call functions and classes from different subdirectories. It will be editable so it will stay up to date as you modify your repo going forward.
-
Create a
srcdirectory in your workspace, and add another subdirectory inside it with the name of your project (the name of this folder is going to be what you call the repo package from. Don't use any special characters in it.). -
Add the following to the
pyproject.tomlfile (replacingmyprojectwith your chosen name`):[build-system] requires = ["setuptools>=42"] build-backend = "setuptools.build_meta" [tool.setuptools.packages.find] where = ['src'] include = ['myproject*'] -
You can add further subdirectories and functions under
myproject- see this repo for an example setup. Note how scripts in different directories of equal hierarchy can call each other. -
Add code to
hello.pythat calls functions undermyproject- see how the import works in the code in this repo. In order to run the app smoothly in Streamlit Community Cloud, it is best to keep the main script to be run by streamlit at the root of your repo. -
Install your package by adding it in terminal:
uv add --editable --dev {YOUR-PROJECT-NAME}. Replace{YOUR-PROJECT-NAME}with the name of your project given inpyproject.toml(so in the case of this repo, the command would beuv add --editable --dev uv-streamlit-setup). -
You can check that the install was successful by running the command
uv pip list- your project package should show up in the list. -
Run the updated
hello.pyusing streamlit in the terminal with the commandstreamlit run hello.py. The streamlit app should launch in you browser and display expected outputs. -
You can now try modifying your code in the package (e.g. add a new function in a module and call it in
hello.py); rerunning the streamlit dashboard (which you can set to 'Always rerun' in the top right) you should find that these updates are reflected without having to reinstall the package.
-
-
Now you can deploy the app! First, make sure to commit and sync your latest code to your repo in Github. Then in the top right corner of your locally-running streamlit app you can click the 'Deploy' button and choose the option to 'Deploy now' to Streamlit Community Cloud. Confirm the repo url, branch and main file path that is shown on the subsequent form, come up with a url prefix for your app. If you do not yet have an account (or are not logged in), you will need to create/log in to your Streamlit account first. Hit the Deploy button and within a few seconds your app will be live!
- From now on, all commits synced to this branch of your repo will be reflected live in the hosted app.
-
Finally, let's set up a basic test framework. We will use pytest, so go ahead and
uv add pytest, and create atestsdirectory to contain your test scripts (see example in this repo). You will also need to updatepyproject.tomlto add apythonpaththat will tell pytest where to look for the functions it needs to test - see commit "add pytest tests" for all the code changes/additions made in this example.
data-sloth/uv-streamlit-setup
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|