Flask project – Create a Joke App with PyJokes


Flask is an incredible choice in the event that you're a Python engineer needing to fabricate a web application. It is a lightweight web framework that is easy to use and understand. Using PyJokes, a Python package with a large number of jokes, we'll show you how to use Flask to create an amusing and interactive joke app in this article.

Installation and Syntax

To get started with our Flask news application, we need to first install Flask and the pyjokes library which will fetch jokes for us randomly

pip install pyjokes flask

Before we dive into the implementation details of our Flask news application, let's first take a look at the basic syntax of Flask. A Flask application typically consists of a series of routes, which are URLs that map to Python functions. Here's an example of a simple Flask route βˆ’

from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
   return 'Hello, World!'

if __name__ == '__main__':
   app.run()

Algorithm

  • Import Flask and pyjokes libraries.

  • Create a Flask application instance.

  • Define the route '/' for the root URL of the application.

  • Define a function called 'index' that will be called when the root URL is requested.

  • Call the 'get_joke' method from the pyjokes library to retrieve a random joke.

  • Render the 'jokes.html' template file and pass the joke as a context variable.

  • Run the application in debug mode if it is being run directly.

  • In the HTML file, display the joke using the Flask variable syntax.

  • Add a button to the HTML file that will reload the page to get a new joke.

Using PyJokes

import pyjokes

# Get a single joke
print(pyjokes.get_joke())

# Get a list of jokes
jokes = pyjokes.get_jokes()
for joke in jokes:
   print(joke)

Joke App Code

Example

app.py

from flask import Flask, render_template
import pyjokes

app = Flask(__name__)

@app.route("/")
def index():
   joke = pyjokes.get_joke()
   return render_template("jokes.html", joke=joke)

if __name__ == "__main__":
   app.run(debug=True)

news.html

Note βˆ’ [Make a folder called β€œtemplates” in the same location as β€œapp.py” and inside that folder store this jokes.html file]

<!doctype html>
<html>
<head>
   <title>Joke App</title>
</head>
<body>
   <h1>Joke App</h1>
   <p>{{ joke }}</p>
   <button onclick="window.location.reload()">Get a new joke</button>
</body>
</html>

Output

Flask application setup βˆ’

The first part of the code imports Flask and PyJokes libraries. The Flask constructor is used to create an instance of the Flask class and is stored in the variable app. This app variable is used to define the routes and views of the web application.

Route definition βˆ’

The route decorator @app.route("/") is used to define the root URL of the application, which is the homepage. The index function is associated with this route, which generates a random joke using the PyJokes library.

Template rendering βˆ’

The render_template method is used to render the jokes.html template file, which is a Jinja2 template. The joke variable generated in the index function is passed to the template using the joke=joke syntax.

Template structure βˆ’

The jokes.html template is a basic HTML file that uses Jinja2 syntax to render the joke variable passed from the Flask application. The title of the page is set to "Joke App", and the heading is also "Joke App". The joke is displayed using the {{ joke }} syntax, which is a placeholder for the joke variable. Finally, a "Get a new joke" button is included to reload the page and generate a new joke.

Model-View-Controller (MVC) architecture βˆ’

It facilitates the coordination of the requests and responses that are delivered and received between the model and the view which is handled by the controller. The HTML template used to display the joke on the web page serves as the view, while the PyJokes module, which generates the random joke, serves as the controller.

Applications

An amusing tool like this may be used for other things besides enjoyment. It may also be used for a variety of practical purposes, including bringing humor to your command-line interface by, for instance, displaying a different joke each time you open the terminal. It may also be used to create a straightforward chatbot that makes a joke anytime a certain word is spoken. This can humanize your chatbot and attract customers' interest.

Conclusion

PyJokes is a fun Flask library for making joke apps that are simple to use and here we exhibited the development of a straightforward application that shows an arbitrary random joke to the client request directly within the browser in just 20 lines of code.

The lightweight and adaptable Flask framework makes it simple to create straightforward web applications like these. We were able to improve the app's humor and user engagement with the assistance of PyJokes. While this application is only a basic model, it exhibits that it is so natural to construct a modern Flask application with Python libraries and make something both useful and fun.

Updated on: 21-Aug-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements