Open In App

Joke App using Bottle Framework – Python

There are many frameworks in python which allows you to create webpage like bottle, flask, django. In this article you will learn how to create simple app bottle.Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

Installation



First we have to install the necessary modules

pip install bottle
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 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:

Categories Included In pyjokes:

Create new directory for project Joke_app

Inside that create a file app.py




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)

Then create new directory and name it as views

Inside that create new file index.tpl




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

To run the app open terminal or cmd

python app.py

Output :-


Article Tags :