Table of contents
1.
Introduction
2.
Installation
3.
pyjokes Library
4.
Creating a Project
5.
Frequently Asked Questions
5.1.
What is Bottle API?
5.2.
What are the two parameters in the get_joke method?
5.3.
What is the return type of the get_joke and get_jokes methods?
5.4.
What two necessary modules are needed for installation?
5.5.
What does WSGI stand for?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Joke App using Bottle Framework

Author yuvatimankar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Many frameworks are available in python that allows us to create web pages like a flask, bottle, and Django. This blog will teach us how to create a simple joke app using the bottle framework.

Bottle Framework

 The bottle is a python micro-framework by which you can build the rest API in minutes. It is distributed as a single file module with only Python Standard Library dependency. Bottle helps in creating web development fastly and easily.

Installation

Firstly, we need to install the necessary modules:

pip install bottle
pip install pyjokes


By importing the pyjokes library, we will get funny one-liners, mostly related to programming.

pyjokes Library


Methods

 

get_joke()

get_jokes

Working get_joke returns a single joke. We get random jokes every time. get_jokes returns a list of jokes. And in this also, we get random jokes every time.
Parameter It consists of two-parameter - language and category. It also consists of the same parameter i.e. language and category.
Return type The return type of this method is a string (str). The return type of this method is the list.

 

Languages supported by pyjokes are as follows:

English - ‘en’

German - ‘de’

Spanish - ‘es’

Galician - ‘gl’

Basque - ‘eu’

Italian - ‘it’

 

Categories that are included in pyjokes are as follows:

 For ninja jokes: ‘neutral (it is the default value)

 For Chris Norris Jokes: ‘chuck.’

 If we want all types of types: ‘all.’

One last category in pyjokes is known as “twister,” which works only for the German language (‘de’). 

Creating a Project

  • Now, we first need to create a project called Joke_app.
  • Inside that, create a file app.py
  • Then, write the following code:

 

from bottle import route, run, template
import pyjokes

@route(‘/’)
Def index();
joke = pyjokes.get_joke()
return template(‘index.tpl’,{‘joke’ : joke})


run(host = ‘localhost’, port = 8080, debug = True)
You can also try this code with Online Python Compiler
Run Code

 

  • Now, create a new directory called “views”
  • Inside that, create a new file “index.tpl”
  • And in an index.tpl write the following code:

 

<html>
       <head>
<title>GFG</title>
        </head>
        <body>
    <h1>{{joke}}</h1>
         </body>
</html>

 

  • To run the application, open cmd and type the following command:

 

python app.py

 

Output:

output

Frequently Asked Questions

What is Bottle API?

The bottle is a python micro-framework by which you can just build the rest API in minutes. It is distributed as a single file module and has only Python Standard Library dependency. Bottle helps in creating web development fastly and easily. 

What are the two parameters in the get_joke method?

get_joke method consists of two parameters: language and property.

What is the return type of the get_joke and get_jokes methods?

The return type of get_joke is a string, whereas the return type of the get_jokes method is the list.

What two necessary modules are needed for installation?

The two modules needed for the joke app are bottle and pyjokes.

What does WSGI stand for?

The Web Server Gateway Interface (pronounced whiskey or WIZ-ghee) is a straightforward calling standard used by web servers to route requests to web applications or frameworks created in the Python programming language.

Conclusion

This article discussed the bottle framework and how to create a simple joke app using Bottle Framework. We started with the introduction of the bottle framework, and then we saw how to install the bottle module and pyjokes module., and then we saw the methods of pyjokes library and the implementation of the project.

To learn more about  Bottle Web Framework, see Bottle DocumentationBottleand Python Frameworks. Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Thank you
Live masterclass