Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to create multiple subdomains from the same flask blueprint
# -*- coding: utf-8 -*-
Showing how to reuse a blueprint for several subdomain
from flask import Flask, Blueprint
app = Flask(__name__)
Note that for subdomains to work reliably, the SERVER_NAME config
variable has to be set. The full syntax is host:port
for example, here we are using localhost:5000.
Something like will be your value, if you are not on localhost
app.config['SERVER_NAME'] = 'localhost:5000'
# The main homepage
def app_index():
return 'Hello, this is the main homepage'
# To replicate the same blueprint for multiple subdomains, the blueprint could
# be created dynamically and returned through a function as defined below.
def subdomain(name, import_name, subdomain, **kwargs):
'''This function accepts all the arguments as you will normally
pass when calling Blueprint, just that subdomain is now required
# formulate the Blueprint with the supplied parameters
subdomain_bp = Blueprint(name, import_name, subdomain=subdomain, **kwargs)
# All the routes goes here
# The simplest index page can be as stated below
def subdomain_index():
return f'''This is subdomain for {name}'''
return subdomain_bp
# Registering the blueprint multiple times:
# Lets' assign different subdomains to different portions or users of our app
if __name__ == '__main__':'localhost', port=5000, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment