Joke App in python using Bottle Framework

A joke app is a web application that delivers humor to users through a collection of jokes, puns, and funny stories. The Bottle framework is a lightweight Python web framework perfect for building simple applications like a joke app. In this tutorial, we'll create a joke app using two different approaches with complete executable code.

What is the Bottle Framework?

Bottle is a lightweight WSGI micro web framework for Python. It provides a simple and efficient way to create web applications with minimal setup. Key features include:

  • Built-in template engine

  • Routing system for URL handling

  • HTTP request and response processing

  • Single-file deployment option

Basic Syntax

Here's the basic structure of a Bottle application ?

from bottle import Bottle, run

app = Bottle()

@app.route('/')
def index():
    return "Hello, World!"

if __name__ == "__main__":
    run(app, host='localhost', port=8080, debug=True)

Algorithm

  • Step 1 Import required modules (Bottle framework and joke sources)

  • Step 2 Create a Bottle application instance

  • Step 3 Define routes for different pages or endpoints

  • Step 4 Implement logic to retrieve and return jokes

  • Step 5 Run the application server and test functionality

Method 1: Using the pyjokes Library

The pyjokes library provides a collection of programming-related jokes. First install it using pip install pyjokes ?

from bottle import Bottle, run
import pyjokes

app = Bottle()

@app.route('/')
def get_joke():
    joke = pyjokes.get_joke()
    return f"<h1>Random Joke</h1><p>{joke}</p><a href='/'>Get Another Joke</a>"

@app.route('/category/<cat>')
def get_joke_by_category(cat):
    try:
        joke = pyjokes.get_joke(category=cat)
        return f"<h1>{cat.title()} Joke</h1><p>{joke}</p><a href='/'>Home</a>"
    except:
        return f"<h1>Error</h1><p>Category '{cat}' not found</p><a href='/'>Home</a>"

if __name__ == "__main__":
    print("Starting joke app server...")
    print("Visit http://localhost:8080 to get jokes!")
    run(app, host='localhost', port=8080, debug=True)

This approach fetches random programming jokes from the pyjokes library and provides category-based filtering.

Method 2: Using a Predefined Joke List

This method uses a hardcoded list of jokes stored within the application ?

from bottle import Bottle, run
import random

app = Bottle()

jokes = [
    "Why do programmers prefer dark mode? Because light attracts bugs!",
    "How many programmers does it take to change a light bulb? None, that's a hardware problem!",
    "Why did the programmer quit his job? He didn't get arrays!",
    "What do you call a programmer from Finland? Nerdic!",
    "Why do Java developers wear glasses? Because they can't C#!"
]

@app.route('/')
def random_joke():
    joke = random.choice(jokes)
    joke_html = f"""
    <html>
    <head><title>Joke App</title></head>
    <body>
        <h1>Programming Joke</h1>
        <p style="font-size: 18px; margin: 20px;">{joke}</p>
        <button onclick="location.reload()">Get Another Joke</button>
        <br><br>
        <a href="/all">View All Jokes</a>
    </body>
    </html>
    """
    return joke_html

@app.route('/all')
def all_jokes():
    jokes_html = "<html><head><title>All Jokes</title></head><body><h1>All Jokes</h1>"
    for i, joke in enumerate(jokes, 1):
        jokes_html += f"<p><b>{i}.</b> {joke}</p>"
    jokes_html += "<a href='/'>Random Joke</a></body></html>"
    return jokes_html

if __name__ == "__main__":
    print("Starting joke app server...")
    print("Visit http://localhost:8080 for random jokes!")
    print("Visit http://localhost:8080/all to see all jokes!")
    run(app, host='localhost', port=8080, debug=True)

This approach provides more control over the joke content and includes additional features like viewing all jokes.

Comparison

Method Pros Cons Best For
pyjokes Library Large joke database, No maintenance External dependency, Programming-focused Quick prototypes
Predefined List Full control, Custom content, No dependencies Limited jokes, Manual updates needed Custom applications

Running the Application

To run either version:

  1. Save the code to a file (e.g., joke_app.py)

  2. Install required packages: pip install bottle pyjokes (for Method 1)

  3. Run: python joke_app.py

  4. Visit http://localhost:8080 in your browser

Conclusion

The Bottle framework makes it easy to create lightweight web applications like joke apps. The pyjokes library offers convenience with ready-made jokes, while predefined lists provide full customization control. Both approaches demonstrate Bottle's simplicity for rapid web development.

Updated on: 2026-03-27T15:08:37+05:30

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements