Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Check if two PDF documents are identical with Python
Next article icon

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 fromuselessfacts.jsph.pl with the help ofGET method, and we will use therequestsand theJSON 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 thePyWebIO 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.

Given below is the complete implementation using the above approach.

Program:

Python
# Import the necessary modulesimportjsonimportrequestsfrompywebio.inputimport*frompywebio.outputimport*frompywebio.sessionimport*defget_fun_fact(_):# Clear the screenclear()# Put HTML content for the fun fact generator headerput_html('<p align="left">''<h2><img src="https://media.geeksforgeeks.org/wp-content/uploads/20210720224119/MessagingHappyicon.png" width="7%"> Fun Fact Generator</h2>''</p>')# URL from where we will fetch the dataurl="https://uselessfacts.jsph.pl/random.json?language=en"# Use GET requestresponse=requests.get(url)# Load the request in json filedata=json.loads(response.text)# We will need 'text' from the datauseless_fact=data['text']# Put the fact in blue color and increase the font sizestyle(put_text(useless_fact),'color:blue; font-size: 30px')# Put the "Click me" buttonput_buttons([dict(label='Click me',value='outline-success',color='outline-success')],onclick=get_fun_fact)# Driver Functionif__name__=='__main__':# Put a heading "Fun Fact Generator"put_html('<p align="left">''<h2><img src="https://media.geeksforgeeks.org/wp-content/uploads/20210720224119/MessagingHappyicon.png" width="7%"> Fun Fact Generator</h2>''</p>')# Hold the session for a long time and put the "Click me" buttonput_buttons([dict(label='Click me',value='outline-success',color='outline-success')],onclick=get_fun_fact)hold()

Output:


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp