- /
- Build
- /
- Quick Starts
- /
- Python
Run a Python App
This quickstart assumes that you have:
- A Deta Space account
- The Space CLI installed and authenticated on your machine
- Python installed on your machine
π‘ Currently, Space supports Python 3.8 and Python 3.9, with plans to add support for Python 3.10 soon. We recommend using a Python version manager like pyenv to install Python on your system. This will allow you to quickly install and use different versions of Python.
Create a Python App
First, create a directory to contain the source code of your app and navigate to it.
mkdir python-app && cd python-app
Next, create a virtual environment to manage the dependencies for your app and activate it with:
python -m venv .venvsource .venv/bin/activate
βΉοΈ It is important to exclude the virtual environment directory from your source code during deployment (
space push
) in order to avoid conflicts and compatibility issues. The Space CLI automatically ignores thevenv
and.venv
directories by default. However, if your virtual environment directory has a different name, you should add it to the.spaceignore
file.
Weβll also install a web framework and a web server. For this guide, weβll use FastAPI with Uvicorn and Flask with Gunicorn, but feel free to use other frameworks and web servers of your choice if you prefer.
In your text editor, create a new file called requirements.txt
, and add the following dependencies into it:
fastapiuvicorn
Flaskgunicorn
Then, install these dependencies to your local virtual environment with:
pip install -r requirements.txt
Create a new file called main.py
. Then, add the following code:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")def root(): return "Hello from Space! π"
from flask import Flask
app = Flask(__name__)
@app.route("/")def root(): return "Hello from Space! π"
This code sets up a basic web server that listens on the port specified by the PORT
environment variable.
π‘ Make sure your app is configured to listen on the port defined by the
PORT
environment variable.
Create a Space Project
Space projects allow you to build, test, and use apps on Deta Space. They are also a (optional) launchpad for releasing them to the public.
space new
You will be prompted to enter a name for your project. The CLI will display a generated configuration for the app and prompt you to confirm.
Once confirmed, the project will be created along with a Spacefile
.
The Spacefile
contains the configuration for your Micro and a .space
directory that stores project information and links it to your project.
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0v: 0micros: - name: python-app src: . engine: python3.9 primary: true
β οΈ Currently, the Spacefile with the generated configuration is based on the old standard of running Python apps on Space, which requires a
main.py
file at the root directory of the Microβs source code with the app instance named asapp
. We recommend updating the configuration in the Spacefile to explicitly define the command to start your app with therun
command:
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0v: 0micros: - name: python-app src: . engine: python3.9 primary: true run: uvicorn main:app
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0v: 0micros: - name: python-app src: . engine: python3.9 primary: true run: gunicorn main:app
β οΈ If the CLI fails to generate a configuration for your app, you can configure it manually. For more information, please refer to the Spacefile reference.
Developing Locally
You can run your app on your local machine, in a way that emulates Space for development. To do so, you need to define a startup command for your appβs development server using the dev
command in the Spacefile.
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0v: 0micros: - name: python-app src: . engine: python3.9 primary: true run: uvicorn main:app dev: .venv/bin/uvicorn main:app --reload
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0v: 0micros: - name: python-app src: . engine: python3.9 primary: true run: gunicorn main:app dev: .venv/bin/flask --app main run
Once you define the dev
command for the Micro in the Spacefile, you can start the development server by running the following command:
space dev
Run it on Space
To deploy your app to Space, simply run:
space push
This will validate your Spacefile, package and upload your source code to the Space build pipeline, and stream logs of the whole process on your terminal. Once the build process is complete, your Builder Instance. Open it in your browser to test and use a live copy of your app on the internet.
π‘ You can use
space push --open
to open the builder instance in your browser after successful deployement and update of the builder instance.
Congratulations! π You have successfully built, deployed and got your first Python app on Space. π