File size: 5,669 Bytes
f6910c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from cProfile import label
import select
from shiny import App, render, reactive,ui

from shiny.ui import h2, tags
#from data_process_func import record_login
import pandas as pd
import io
import csv
import asyncio
#import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
from supp import *

curr_df = get_survey_responses()




app_ui = ui.page_fluid(
    ui.panel_title("Login"),
    ui.row(
        ui.input_text("username","Username:",placeholder="username"),
        ui.input_password("password","Password:"),
        ui.input_action_button("submit","Log in/Log out",width='200px'),
        ui.input_action_button("show_guide","Guideline",width='200px'),

    ),

    ui.row(
        ui.column(
            6,
            ui.panel_well( 
                ui.output_text("out_text1"),
                #ui.download_button("save_result","Download Table",width='150px'),
            )
            
        )    
    ),

)


def server(input, output, session):
    login_status = -1



    @output
    @render.text
    def out_text1():
        return "Current Responses:"


    @output
    @render.ui
    @reactive.Effect
    @reactive.event(input.submit)
    def submit():
        
        nonlocal login_status
        #print("The log in status is "+str(login_status))
        if login_status==-1:
            username = input.username()
            password = str(input.password())
            correct_or_not = checkpassword(username,password)        
            if correct_or_not ==1:
                ui.remove_ui(selector="div:has(> #username)")
                ui.remove_ui(selector="div:has(> #password)")
                login_status=1
                
                
                ui.insert_ui( 
                        ui.download_button("save_result","Download Table",width='150px'),              
                        selector="#out_text1",
                        where="afterEnd"
                    )
                ui.insert_ui( 
                        ui.output_plot("sec_plot1"),              
                        selector="#out_text1",
                        where="afterEnd"
                    )
                ui.insert_ui( 
                        ui.output_plot("sec_plot2"),              
                        selector="#out_text1",
                        where="afterEnd"
                    )
                """
                """
        #log out!
        elif login_status ==1:
            ui.remove_ui(selector="div:has(> #sec_plot1")
            ui.remove_ui(selector="div:has(> #sec_plot2)")
            
            ui.remove_ui(selector="div:has(> #save_result)")
            ui.insert_ui(
                ui.input_text("username","Username:",placeholder="username"),
                selector="#submit",
                where="beforeBegin",
                
            )
            ui.insert_ui(
                ui.input_password("password","Password"),
                selector="#submit",
                where="beforeBegin",
            )
            ui.insert_ui(
                ui.row(
                    ui.column(
                        5,
                        ui.panel_well(
                            ui.output_text("out_text1"),
                        )
                    )
                ),
                selector="#show_guide",
                where="afterend"
            )
            
            """
            
            ui.insert_ui(
                ui.output_text("out_text1"),
                selector ="#Main Page",
                where="afterEnd"
                
            )
            """
            """
            
            
            ui.insert_ui(
                ui.output_text("text_c1"),
                selector ="#submit",
                where ="afterEnd",
            )
            """

            


            login_status=-1



    @output
    @render.plot
    def sec_plot1():
        nonlocal login_status

        from matplotlib import pyplot as plt
        num1 = get_response_data(curr_df,"Principal","INFO_1","INFO_5","INFO_6")
        print("current status is "+str(login_status))
        
        fig,ax = plt.subplots()

        finished_resp = 0
        for k,v in num1.items():
            finished_resp+=len(v)
        unfinish = get_totalsize()-finished_resp
        c1 = "Principal, Response: "+str(finished_resp)+", Not Response: "+str(unfinish)
        ax.pie([finished_resp,unfinish],labels = ["Finished","Not Finished"],autopct = '%2.2f%%')
        ax.set_title(c1)

        return fig


    @output
    @render.plot
    def sec_plot2():
        from matplotlib import pyplot as plt
        num1 = get_response_data(curr_df,"Educator","INFO_1","INFO_5","INFO_6")
        finished_resp = 0
        for k,v in num1.items():
            finished_resp+=len(v)
        unfinish = get_totalsize()-finished_resp
        fig,ax = plt.subplots()
        ax.pie([finished_resp,unfinish],labels = ["Finished","Not Finished"],autopct = '%2.2f%%')
        c1 = "Educator, Response: "+str(finished_resp)+", Not Response: "+str(unfinish)
        ax.set_title(c1)

        return fig






        
    @session.download(
        filename = f"result.csv"
    )
    async def save_result():
        this_df = get_responses_check_df(curr_df,"INFO_1","INFO_5","INFO_6")
        sz1,sz2 = this_df.shape
        df_col = this_df.columns

        yield df_col[0]+ ","+df_col[1]+","+df_col[2]+","+df_col[3]+ ","+df_col[4]+"\n"
        for i in range(sz1):
            yield str(this_df.iloc[i,0])+","+str(this_df.iloc[i,1])+","+str(this_df.iloc[i,2])+","+str(this_df.iloc[i,3])+","+str(this_df.iloc[i,4])+"\n"



app = App(app_ui, server)