Skip to content
Related Articles

Related Articles

Improve Article

Flask project – Create a Joke App with PyJokes

  • Last Updated : 26 Nov, 2020
Geek Week

Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries. Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.

For creating joke app we need two libraries flask and pyjokes.

Installation –

First we install flask

pip install flask

Second library installation

pip install pyjokes

You  get funny one-liner, mostly related to programming by using just importing a library known as pyjokes.



Some Methods Of pyjokes Library

There are two major methods in pyjokes- get_joke() and get_jokes().  

  • get_joke()– It only returns one joke. We get random joke each time.
  • Parameters – There are two parameters- language and category. You can choose from the language and category above. 
  • Return Type – It return string type (str).
  • get_jokes() – Here, we get a list of jokes.  
  • Parameter– The parameters are same as above- language and category.
  • Return type– list. 

Languages Supported By pyjokes:

  • English – ‘en’
  • German – ‘de’
  • Spanish – ‘es’
  • Galician – ‘gl’
  • Basque – ‘eu’
  • Italian – ‘it’

Categories Included In pyjokes:

  • For geeky jokes -’neutral’ (It is chosen by default)
  • For Chris Norris Jokes – ‘chuck’.
  • If you want all type of jokes – ‘all’
  • There is one more category known as ‘twister’ which only works for the German Language (‘de’). This mostly includes tongue twister.

Creating Flask Project – 

I have created a file and name it as app.py

app.py

Python3




from flask import Flask
import pyjokes
  
app=Flask(__name__)
  
  
@app.route("/")
def home():
    joke=pyjokes.get_joke()  #It only returns one joke. We get random joke each time. 
    return f'<h2>{joke}</h2>'
  
@app.route("/MultipleJokes")
def jokes():
    jokes=pyjokes.get_jokes()  #Here, we get a list of jokes.  
    return f'<h2>{jokes}</h2>'
  
if __name__ == "__main__":
    app.run(debug=True)

To run flask app you need run this command in your command prompt or terminal:

For windows

python app.py

For ubuntu

python3 app.py

After running the app you will get output like this 

Output on “http://127.0.0.1:5000”

Output on “http://127.0.0.1:5000/MultipleJokes”

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :