Run a Python App

This quickstart assumes that you have:

πŸ’‘ 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.

Terminal window
mkdir python-app && cd python-app

Next, create a virtual environment to manage the dependencies for your app and activate it with:

Terminal window
python -m venv .venv
source .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 the venv 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:

fastapi
uvicorn

Then, install these dependencies to your local virtual environment with:

Terminal window
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! πŸš€"

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.

Terminal window
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/v0
v: 0
micros:
- 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 as app. We recommend updating the configuration in the Spacefile to explicitly define the command to start your app with the run command:

# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
micros:
- name: python-app
src: .
engine: python3.9
primary: true
run: uvicorn 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/v0
v: 0
micros:
- name: python-app
src: .
engine: python3.9
primary: true
run: uvicorn main:app
dev: .venv/bin/uvicorn main:app --reload

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. πŸš€