Empower Your Development: FastAPI on Mobile

AbdulquddusAbdulquddus
4 min read

In today's digital age, the ability to perform back-end development directly from a mobile device offers unparalleled flexibility and convenience. This article will guide you through the process of configuring your mobile device for back-end development, with a special focus on FastAPI. By setting up this environment, you'll be equipped to efficiently manage development tasks that require terminal access, all from the palm of your hand. Whether you're a seasoned developer or just starting out, this setup will empower you to harness the full potential of mobile technology in your development workflow.

Apps to download

Setting up Termux

Open Termux and run the following commands

1. Select repo (optional)

This command allows you to choose from available mirrors, which are servers that host the packages Termux will download. Selecting a mirror can help improve download speeds and reliability.

termux-change-repo

This brings up a pop-up for you to choose from the available mirrors. These mirrors are where Termux will download packages from. Just keep pressing enter until the pop-up closes.

Termux repo change

2. Setting up storage

termux-setup-storage

This command creates a shared storage space between Termux and your phone, enabling Termux to access your phone's files and folders.

3. Update and Upgrade environment packages

apt update && apt upgrade -y

Running this command updates the package lists and upgrades all installed packages to their latest versions, ensuring your environment is up to date.

4. Installing required tools

pkg install python rust git -y

This command installs Python, Rust, and Git. Python is necessary for running FastAPI, Rust is required for compiling some dependencies, and Git is used for version control.

python --version
rustc --version
cargo  --version
git --version

These commands will check if all the packages are installed successfully. You can either run them line by line or just copy and paste them all at once.

5. Create a symlink to a folder that'll be holding your projects

mkdir ~/storage/shared/MyDevFolder

This will create a folder called MyDevFolder in your main storage. On some Android devices, /storage/shared is /storage/emulated/0.

ln -s ~/storage/shared/MyDevFolder ~/MyDevFolder

This command creates a symbolic link (shortcut) to MyDevFolder in your main storage, making it easier to access your project files from Termux

Create an empty project folder in MyDevFolder using:

mkdir MyDevFolder/testProject

6. Setting up a python virtual environment

Termux doesn't let you install Python packages directly in the environment, so you need to create a virtual environment for your projects. This ensures that all the packages you install will be specific to each project and won't interfere with your existing Python installation or the global environment.

python -m  venv .venv/testProject

This command will create a virtual environment stored in the .venv folder for the specific testProject.

To activate the virtual environment, run:

. ~/.venv/testProject/bin/activate

Change directory to the project folder:

cd MyDevFolder/testProject

and Install FastAPI with:

pip install "fastapi[standard]"

This command installs FastAPI and its dependencies within the virtual environment, allowing you to develop and run FastAPI applications.

The installation and build process might take some time to complete. Please wait until it finishes, then continue to the next phase.

Using Acode as a Text Editor

You can edit and write your code directly in Termux using text editors like nano, Vim, or micro. To make things easier, we will use Acode, which has an interface similar to desktop editors like VS Code, but it's designed for mobile.

1. Add MyDevFolder to the path

Add your Dev folder to Acode so you can easily access all your projects.

  • Click the three lines in the upper left corner

  • Tap “Open Folder”

  • Click on “Add a storage”

  • Click “Select folder” and choose “MyDevFolder”

  • Click “Use this folder” (depending on your device)

  • Select “Allow” on the next pop-up, then “Ok”

  • Select “MyDevFolder” in Acode, and click the ✅ in the bottom corner

You should have this:

Acode Side Nav

2. Create a main.py file in testProject

Press and hold on "testProject," then choose "New file" and name it "main.py."

Add this starter code to the file:

from fastapi import FastAPI

app = FastAPI()

@app.get('/')
async def welcome():
    return {"message": "Welcome to FastAPI"}

Save the changes and return to Termux.

Running FastAPI development server

Run this command in your Termux terminal:

fastapi dev

You should see an output like this:

fast dev sever running

Copy and paste the link into your browser, and you should see this:

Your API's documentation page

.

Congratulations! 🎉 Your device is now fully set up to run FastAPI development.

In conclusion, setting up FastAPI on your mobile device provides a unique blend of flexibility, convenience, and efficiency, allowing you to manage back-end development tasks from virtually anywhere. This setup not only enhances your ability to work on-the-go but also serves as a cost-effective solution for developers looking to maximize their productivity without additional hardware investments. With this newfound capability, you're encouraged to explore further development possibilities, experiment with new APIs, and collaborate on projects, unlocking new avenues for creativity and innovation in your workflow.

0
Subscribe to my newsletter

Read articles from Abdulquddus directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abdulquddus
Abdulquddus