File size: 3,071 Bytes
cef1936
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import panel as pn
import requests
import pandas as pd
pn.extension()


# In[ ]:


def get_data(num_questions, difficulty, category):
    url = f"https://opentdb.com/api.php?amount={num_questions}&category={category_match[category]}&difficulty={difficulty}&type=boolean"
    df = pd.DataFrame(
        requests.get(url).json()['results']
    )
    return df 


# In[ ]:


category = pn.widgets.Select(
    name='Category',
    options=[
        'General Knowledge',
        'Film',
        'Music',
        'Video Games',
        'Science & Nature',
        'Computers',
        'Geography',
        'History',
        'Politics',
        'Animals',
        'Japanese Anime & Manga'
    ], 
    value='General Knowledge'
)
category


# In[ ]:


category_match = {
   'General Knowledge': 9,
   'Books': 10,
   'Film': 11,
   'Music': 12,
   'Musicals & Theatres': 13,
   'Television': 14,
   'Video Games': 15,
   'Board Games': 16,
   'Science & Nature': 17,
   'Computers': 18,
   'Mathematics': 19,
   'Mythology': 20,
   'Sports': 21,
   'Geography': 22,
   'History': 23,
   'Politics': 24,
   'Art': 25,
   'Celebrities': 26,
   'Animals': 27,
   'Vehicles': 28,
   'Comics': 29,
   'Gadgets': 30,
   'Japanese Anime & Manga': 31,
   'Cartoon & Animations': 32
}


# In[ ]:


difficulty = pn.widgets.Select(
    name='Difficulty',
    options=['easy', 'medium', 'hard'], 
    value='easy'
)
difficulty


# In[ ]:


num_questions = pn.widgets.DiscreteSlider(
    name='Number of Questions', 
    options=[5, 10, 15, 20], value=5
)
num_questions


# In[ ]:


def question_list(i, df):

    button_true = pn.widgets.Button(name='True')
    button_false = pn.widgets.Button(name='False')
 
    text = pn.widgets.StaticText(value='')

    def processing_button_true(event):
        if df.correct_answer[i] == 'True': 
            text.value = 'Correct!'
        else:
            text.value = 'Incorrect!'

    def processing_button_false(event):
        if df.correct_answer[i] == 'False': 
            text.value = 'Correct!'
        else:
            text.value = 'Incorrect!'

    button_true.on_click(processing_button_true)
    button_false.on_click(processing_button_false)
    return pn.Column(
        pn.pane.Markdown(f"""
         
        #Question {i+1}:
        ### {df.question[i]}
        """),

        pn.Row(button_true,button_false), 
        text)


# In[ ]:


def get_data_and_questions(num_questions, difficulty, category):
    df = get_data(num_questions, difficulty, category)
    question_pane = [question_list(i, df) for i in range(len(df))]
    trivia_pane = pn.Column(*question_pane)
    return trivia_pane 


# In[ ]:


interactive = pn.bind(get_data_and_questions, num_questions, difficulty, category)


# In[ ]:


# Layout using Template
template = pn.template.FastListTemplate(
    title='Trivia Game', 
    sidebar=[num_questions, difficulty, category],
    main=[interactive],
    accent_base_color="#88d8b0",
    header_background="#88d8b0",
)
template.servable()


# In[ ]: