Open In App
Related Articles

Fun Fact Generator Web App in Python

Improve Article
Save Article
Like Article

In this article, we will discuss how to create a Fun Fact Generator Web App in Python using the PyWebio module. Essentially, it will create interesting facts at random and display them on the web interface. This script will retrieve data from with the help of GET method, and we will use the requests and the JSON module to acquire and load the data respectively. After the data has been loaded, just pass the text within the dictionary so that we can only get the jokes. For creating a simple and interactive interface on the web, we will use the PyWebIO module.

PyWebIO contains functions for getting user input and output on the browser, turning the browser into a “rich text terminal”, and can be used to build simple web applications or browser-based GUI applications. With this module, anyone can produce a web application without any prior knowledge or overhead of HTML and JS.

It can be installed using the below command:

pip install pywebio

Getting Started

Follow the below steps to create a fun factor generator web app.

  • Import all the required modules
  • Send GET requests with the requests.request() method, and use the json.loads() function to parse a valid JSON message and transform it to a Python Dictionary. Since the requests function will create a dictionary of items, and we only need text, so we will pass response.text inside the json module.
  • Now, because we need the text, i.e. Facts, we will pass the text within the data list and print the facts using the style function defined in the pywebio module. Because data is a dictionary of random jokes involving several useless items, we will just get the text section.

Given below is the complete implementation using the above approach.



# import the following modules
import json
import requests
from pywebio.input import *
from pywebio.output import *
from pywebio.session import *
def get_fun_fact(_):
    # Clear the above screen
    put_html("<p align=""left""><h2><img src=""https://\
             20210720224119/MessagingHappyicon.png"" width=""7%"">  \
             Fun Fact Generator</h2></p>
    # URL from where we will fetch the data
    # Use GET request
    response = requests.request("GET", url)  
    # Load the request in json file
    data = json.loads(response.text)
    # we will need 'text' from the list, so 
    # pass 'text' in the list
    useless_fact = data['text']
    # Put the facts in the blue colour
    # Put the click me button
    style(put_text(useless_fact), 'color:blue; font-size: 30px')
        [dict(label='Click me', value='outline-success'
              color='outline-success')], onclick=get_fun_fact)  
# Driver Function
if __name__ == '__main__':
    # Put a heading "Fun Fact Generator"
    put_html("<p align=""left""><h2><img src=""https://media.\
             /MessagingHappyicon.png"" width=""7%"">  \
             Fun Fact Generator</h2></p>
    # hold the session for long time
    # Put a Click me button
        [dict(label='Click me', value='outline-success'
              color='outline-success')], onclick=get_fun_fact)  


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 May, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials