Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

12/29/23, 2:36 AM Free Hosting a Python API.

Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Get unlimited access to the best of Medium for less than $1/week. Become a member

Free Hosting a Python API


Trần Hoàng Long · Follow
4 min read · Jan 30

62 1

My choice for free python hosting 🎖

Foreword

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 1/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

A few months ago, I encountered a problem while trying to deploy my


backend API for a demo of my side project. Which free service would host
my python backend?

The criterias/downsides that I had were:

Must be fully free with no credit card requirement (student problem 😭)


→ So no AWS, Google or Azure or any other virtual machine services

I have a database so the server must be able to make DB connections


instead of just communicating with frontend

Of course speed is not my concern since we’re going dirt-tier anyways

Lastly, of course, the server must support python (duh!) (Django to be


specific)

If you had some of the similar problems, this blog could be for you. I’ll
discuss some of my findings and workarounds below

😓 It was suprisingly hard for me to find a completely free python hosting


service. Took lots of trial/error. That’s why I write this post for anyone that
likes to save their time digging.

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 2/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

TOC
· Foreword
· TOC
· Project Description
∘ Database
∘ Backend
∘ Frontend
· Hosting Service
· Render Hosting
∘ Build server
∘ Environment Variables
∘ Run server
· Demo
· Final words

Project Description
So here’s a quick layout of my project at the time, called Geo-covid , which is
basically a visualization website for the covid problem in USA, which
consists of 3 parts

Database

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 3/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

MongoDB is my goto database here due to the implementation simplicity and


also the Mongo Atlas support for free DB hosting

‼️ This (db connection from/to the backend) proven itself to be the trickiest
part of the server hosting process

Backend
My API implemented with Python and Django REST API

Why Django REST and not Fast API? Well cause I just wanna try Django out
for once.

Frontend
Web visualization with the help of Tailwind CSS and D3.js (graph) and
Leaflet (map)

We don’t focus on the frontend here since free hosting for frontend JS is
everywhere and it only make simple request back/forth to our API server.

Hosting Service

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 4/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

After some trial and error, the two contenders that were the most promising
were:

1. render.com

2. pythonanywhere.com

But in the end, pythonanywhere fell short of my need, despite being the more
comprehensive and dev friendly service. All because of the previously
mention DB connection problem 😮‍💨

You see on the free tier, pythonanywhere only allow external connection
under the https protocol, so my Mongo Atlas connection is of mongodb://

protocol which would simply not work (Link)

Thankfully, render later prove to work properly, which is good enough 🙏

Render Hosting
The hosting procedure is very detaily describe on Render 's documentation
(Django Quickstart Doc). So I’m just going to point out notable things from
my project

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 5/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

The source code for my backend that’s deployed could be found on my


production branch. And in that branch, I din’t modify any code or add files
to config deployment. All were done on Render itself (due to the simplicity of
my project)

Important steps are:

1. Build: Setup python environment and (optional) build static files

2. Setup production environment variables

3. Run server

Build server
Python env

All we need here is a requirement.txt file that we’lll use to install packages
when building our source code with pip install -r requirement.txt

Static files

Static files for Django server API GUI could be generated with python

manage.py collectstatic

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 6/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Build

We put all the needed commands for server preping in the Build Command

option for Render (found in Settings > Build & Deploy > Build Command ). I set
it as

pip install -r requirements.txt && python manage.py collectstatic

Environment Variables
Found in Environment tab

First option to set is the PYTHON_VERSION (I used 3.9.0 )

For other variables related to my backend, I uploaded my .env file directly


to Render instead.

This includes:

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 7/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

DEBUG=FALSE
DB_URL="mongodb+srv://<username>:<password>@<cluster_address>.mongodb.net/?retry
DEPLOY_ENV=production

Here’s how the environment settings looks like

Render’s Project Environment Settings

❓For anyone wondering how in the code I connected to Atlas , here’s that
snippet
https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 8/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

from pymongo import MongoClient


client = MongoClient(
host=DB_URL
)
# Example DB URL:
# "mongodb+srv://username:pasword@cluster1.2o4txh.mongodb.net/?retryWrites=true&

Run server
Search Settings > Build & Deploy > Run Command Write

gunicorn --bind :$PORT --workers 4 -k uvicorn.workers.UvicornWorker geo_covid.as

Key takeaway is that I ran it with gunicorn and some extra port, worker
settings.

After all the settings, just run deploy with latest commit 🤲

Demo

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 9/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

After getting the DB up and then the backend API online, I try making
connections to it from my frontend and everything is nice/clean 👩‍🍳🥳

The server might not be online anymore but as of now, my frontend is still
online and you can visit it at https://geo-covid-frontend.web.app/

Demo

Backend source code can be found here and front-end here

Final words
https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 10/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Those are my findings while tring to get my side project online. It’s quite
messy and erroneous overall but I’m quite happy that it’s online.

I’ll try to get a creditcard soon ☠️

But please, do comment your thought on my topic and clap if it helped in any
ways. Would love to hear from you ⭐

Thanks for reading and have a nice day 😎

Deployment Python Render Rest Api Production

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 11/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Written by Trần Hoàng Long Follow

69 Followers

Aspiring Data Scientist

More from Trần Hoàng Long

Trần Hoàng Long in Learning SQL Trần Hoàng Long

SQL Window Function Visualized Stream — Pitfalls of HTTP requests


Wow exactly window functions’ window is Does HTTP use data stream for data transfer,
constructed, moved and calculated?… how? What’s the difference between “stream…

6 min read · Jun 5, 2022 8 min read · Sep 4

246 9 1

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 12/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Trần Hoàng Long in Dev Genius Trần Hoàng Long

Spark Cluster with Virtual Box, Path Of Exile Economy — A Time


Anaconda and Jupyter — The guide Series Analysis
A extra detailed guide on setting up Spark A Path of Exile Market Analysis, leveraging
cluster on VirtualBox VM with Anaconda,… Time Series Analysis, Machine Learning…

15 min read · Nov 3, 2022 9 min read · Nov 11, 2022

15 15

See all from Trần Hoàng Long

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 13/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Recommended from Medium

Sabrine Bendimerad GO-FOR-IT in AWS in Plain English

Deploying a Machine Learning Deploying a Flask Application on


Model with Flask and Streamlit o… EC2
In this article, we will guide you through the Flask is a popular web framework for Python,
process of deploying your predictions using… known for its simplicity and flexibility.…

5 min read · Jul 15 6 min read · Sep 23

74 1 137 1

Lists

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 14/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Coding & Development Predictive Modeling w/


11 stories · 346 saves Python
20 stories · 730 saves

Practical Guides to Machine ChatGPT


Learning 23 stories · 353 saves
10 stories · 838 saves

Abdulaziz Pulatjonov in DevOps.dev Pytech Academy

Free Hosting for Your Telegram Streamlit App Deployment: Step-


Bot: It’s Easier Than You Think by-Step Guide to Streamlit Cloud
Let’s talk about how to create and run your Streamlit, a Python library, has gained
custom Telegram bot entirely for free 24/7. immense popularity for its ability to transfor…

7 min read · Jul 2 3 min read · Aug 18

52 1 205

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 15/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Sandyjtech Dhwajgupta

Building a Full Stack App with Build a Node.js Server in 5 Minutes:


Flask, React, MySQL Quick and Easy Server Setup
Creating a full-stack app is a daunting task A beginner guide to creating a server.
and I remember feeling overwhelmed and lo…

6 min read · Sep 1 2 min read · Jun 30

15 7 2

See more recommendations

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 16/17
12/29/23, 2:36 AM Free Hosting a Python API. Hosting a Python Django Backend REST… | by Trần Hoàng Long | Medium

Help Status About Careers Blog Privacy Terms Text to speech Teams

https://medium.com/@produde/free-hosting-a-python-api-632968f14b20 17/17

You might also like