Build your own URL shortener like bit.ly — in Python. You'll learn Flask web framework basics, how HTTP redirects work, and how to build and serve a simple web form. No database required.
A local web app where you paste a long URL, get a short code back (like /go/abc123), and anyone who visits that short URL gets redirected to the original. Runs in your browser, powered by Flask.
Flask is Python's most popular micro-framework for building web apps. Once you understand it, you can build APIs, dashboards, and full web apps. Let's start with something practical.
Flask is a micro web framework — small, fast, and beginner-friendly. Install it:
That's the only dependency you need. Flask comes with a built-in development server, so you don't need Apache or Nginx.
Create a file called app.py. Here's the minimum Flask app:
Run it with python app.py and open http://127.0.0.1:5000 in your browser. You should see "Hello from Flask!".
We'll store URLs in a Python dictionary (for simplicity). Add this above the routes:
Replace the home route with a real HTML form:
render_template_string() lets us embed a Jinja2 template directly in the Python file — no separate HTML files needed for small apps.
This route handles the form submission and creates the short code:
The form submits a POST request to /shorten, which generates a 6-character code, stores the mapping, and shows the short URL back to the user.
This is the most important part — when someone visits a short URL, redirect them:
/go/<code> is a dynamic route — Flask captures whatever comes after /go/ and passes it as the code argument. redirect() sends the browser to the original URL with a 302 status.
Start the server:
Open http://127.0.0.1:5000. Paste any long URL and click Shorten. Copy the short URL and paste it in a new tab — you'll be redirected instantly.
sqlite3 or Flask-SQLAlchemy.You built a working URL shortener with Flask — complete with a web form, short code generation, and HTTP redirects. You now understand the core of how every web framework works.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.