Running Django Application via Colab & Exposing with Ngrok
Are you eager to run your Django application seamlessly via Colab and make it accessible to the world ? Look no further! In this tutorial, we will guide you through the process of setting up your Django project on Google Colab and exposing it to the internet using Ngrok.
Before we dive into the steps, make sure you have a Django project ready to go. If not, don’t worry! You can quickly create one using the Django framework.
Let’s get started with the steps:
Step 1: Install Django and Ngrok
First things first, we need to install Django and Pyngrok libraries. Run the following commands in your Colab notebook:
!pip install django
!pip install pyngrok
Step 2: Clone Your Django Project
If your Django project is hosted on a Git repository, you can clone it directly into your Colab environment using the following command:
!git clone {django_project_url}
Step 3: Navigate to Your Project Folder
Once your project is cloned, navigate to its directory using:
%cd {project_folder}
Step 4: Run Database Migrations
To ensure that your database is up-to-date, run the following commands:
!python manage.py makemigrations
!python manage.py migrate
Step 5: Start Django Server
Now, let’s start the Django development server in a separate thread:
import threading
def run_server():
!python manage.py runserver
# Start the Django development server in a separate thread
server_thread = threading.Thread(target=run_server)
server_thread.start()
Step 6: Setup Ngrok Tunnel
Finally, it’s time to expose your Django application to the internet using Ngrok. Run the following code:
from pyngrok import ngrok
# Function to setup ngrok tunnel
def setup_ngrok():
ngrok.set_auth_token("your_ngrok_auth_token")
public_url = ngrok.connect(addr="8000", proto="http")
print("Ngrok Tunnel URL:", public_url)
# Setup ngrok tunnel in a separate thread
ngrok_thread = threading.Thread(target=setup_ngrok)
ngrok_thread.start()
Replace "your_ngrok_auth_token"
with your Ngrok authentication token.
That’s it! Your Django application is now up and running on Colab, and thanks to Ngrok, it’s accessible via a public URL.
Notebook Link & Further Resources
For a detailed walkthrough and runnable code snippets, check out the full tutorial on Google Colab.
And if you prefer a visual guide, watch our YouTube tutorial here.
Happy coding! 🚀