-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFun_factGenrator.py
More file actions
57 lines (47 loc) · 2.34 KB
/
Fun_factGenrator.py
File metadata and controls
57 lines (47 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Import the necessary modules
import json #json is module which is transfer data from server to python code
import requests #send and receive the requests form web
from pywebio.input import * #import all input funtion
from pywebio.output import * #all outpur funtion
from pywebio.session import * #import session utilities
def get_fun_fact(_): #This comes from PyWebIO.
#PyWebIO passes a parameter to your function automatically when a button is clicked.
#But you don’t need that parameter in your code.So Python developers use _ to say:
# Clear the screen
clear()
# Put HTML content for the fun fact generator header
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>'
) #writes raw HTML to the PyWebIO output area. Here it displays a header (<h2>) with an image and the text "Fun Fact Generator".
# URL from where we will fetch the data The api return json when you request that
url = "https://uselessfacts.jsph.pl/random.json?language=en"
# Use GET request
response = requests.get(url)
# Load the request in json file
data = json.loads(response.text)
# We will need 'text' from the data
useless_fact = data['text']
#useless_fact = data['text'] — extracts the "text" field from the JSON object; that field holds the actual fact string.
# Put the fact in blue color and increase the font size
style(put_text(useless_fact), 'color:blue; font-size: 30px')
# Put the "Click me" button
put_buttons(
[dict(label='Click me', value='outline-success', color='outline-success')],
onclick=get_fun_fact
)
# Driver Function
if __name__ == '__main__': #The block inside runs only when you execute this file directly (not when importing it as a module).
# 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" button
put_buttons(
[dict(label='Click me', value='outline-success', color='outline-success')],
onclick=get_fun_fact
)
hold()