Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Schedule_Buddy
|
| 3 |
+
app_file: main.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 5.7.1
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
main.py
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import copy
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from collections import Counter
|
| 6 |
+
import random
|
| 7 |
+
import re
|
| 8 |
+
from datetime import date
|
| 9 |
+
import supabase
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
### LOAD PRIVATE SPACE ###
|
| 14 |
+
url = "Juggling/Schedule_Buddy_Version_2.0"
|
| 15 |
+
generator = gr.load(url, src="spaces", token=os['TOKEN'])
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
### CONSTANTS ###
|
| 20 |
+
NAME_COL = 'Juggler_Name'
|
| 21 |
+
NUM_WORKSHOPS_COL = 'Num_Workshops'
|
| 22 |
+
AVAIL_COL = 'Availability'
|
| 23 |
+
DESCRIP_COL = 'Workshop_Descriptions'
|
| 24 |
+
EMAIL_COL = 'Email'
|
| 25 |
+
DELIMITER = ';'
|
| 26 |
+
ALERT_TIME = None # leave warnings on screen indefinitely
|
| 27 |
+
FORM_NOT_FOUND = 'Form not found'
|
| 28 |
+
INCORRECT_PASSWORD = "The password is incorrect. Please check the password and try again. If you don't remember your password, please email jugglinggym@gmail.com."
|
| 29 |
+
NUM_ROWS = 1
|
| 30 |
+
NUM_COLS_SCHEDULES = 2
|
| 31 |
+
NUM_COLS_ALL_RESPONSES = 4
|
| 32 |
+
MIN_LENGTH = 6
|
| 33 |
+
NUM_RESULTS = 10 # randomly get {NUM_RESULTS} results
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
theme = gr.themes.Soft(
|
| 37 |
+
primary_hue="cyan",
|
| 38 |
+
secondary_hue="pink",
|
| 39 |
+
font=[gr.themes.GoogleFont('sans-serif'), 'ui-sans-serif', 'system-ui', 'Montserrat'],
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
### Connect to Supabase ###
|
| 43 |
+
#URL = os.environ['URL']
|
| 44 |
+
#API_KEY = os.environ['API_KEY']
|
| 45 |
+
URL = 'https://ubngctgvhjgxkvimdmri.supabase.co'
|
| 46 |
+
API_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InVibmdjdGd2aGpneGt2aW1kbXJpIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzQ5MjAwOTQsImV4cCI6MjA1MDQ5NjA5NH0.NtGdfP8GYNuYdPdsaLW5GjgfB0_7Q1kNBIDJtPhO8nY'
|
| 47 |
+
client = supabase.create_client(URL, API_KEY)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
### DEFINE FUNCTIONS ###
|
| 51 |
+
## Multi-purpose function ##
|
| 52 |
+
'''
|
| 53 |
+
Returns a lowercased and stripped version of the schedule name.
|
| 54 |
+
Returns: str
|
| 55 |
+
'''
|
| 56 |
+
def standardize(schedule_name: str):
|
| 57 |
+
return schedule_name.lower().strip()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
## Function to make a form ##
|
| 61 |
+
'''
|
| 62 |
+
Makes a form and pushes it to Supabase.
|
| 63 |
+
Returns: None
|
| 64 |
+
'''
|
| 65 |
+
def make_form(email: str, schedule_name: str, password_1: str, password_2: str, capacity: int, slots: list) -> str:
|
| 66 |
+
# Error handling
|
| 67 |
+
if len(email) == 0:
|
| 68 |
+
return gr.Warning('', ALERT_TIME, title="Please enter an email address")
|
| 69 |
+
|
| 70 |
+
if len(schedule_name) == 0:
|
| 71 |
+
return gr.Warning('', ALERT_TIME, title=f"Please enter the form name.")
|
| 72 |
+
|
| 73 |
+
if password_1 != password_2:
|
| 74 |
+
return gr.Warning('', ALERT_TIME, title=f"The passwords don't match. Password 1 is \"{password_1}\" and Password 2 is \"{password_2}\".")
|
| 75 |
+
|
| 76 |
+
if len(password_1) < MIN_LENGTH:
|
| 77 |
+
return gr.Warning('', ALERT_TIME, title=f"Please make a password that is at least {MIN_LENGTH} characters.")
|
| 78 |
+
|
| 79 |
+
if capacity == 0:
|
| 80 |
+
return gr.Warning('', ALERT_TIME, title=f"Please enter the capacity (how many people can teach per timeslot). It must be greater than zero.")
|
| 81 |
+
|
| 82 |
+
if capacity < 0:
|
| 83 |
+
return gr.Warning('', ALERT_TIME, title=f"The capacity (number of people who can teach per timeslot) must be greater than zero.")
|
| 84 |
+
|
| 85 |
+
if len(slots) == 0:
|
| 86 |
+
return gr.Warning('', ALERT_TIME, title="Please enter at least one timeslot. Make sure to press \"Enter\" after each one!")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# Check if schedule name already exists
|
| 90 |
+
existing_forms = []
|
| 91 |
+
response = client.table('Forms').select('form_name').execute()
|
| 92 |
+
for elem in response.data:
|
| 93 |
+
existing_forms.append(elem['form_name'])
|
| 94 |
+
|
| 95 |
+
if schedule_name in existing_forms:
|
| 96 |
+
return gr.Warning('', ALERT_TIME, title=f"The form name \"{schedule_name}\" already exists. Please choose a different name.")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# Push to Supabase
|
| 100 |
+
new_slots = [elem['name'] for elem in slots]
|
| 101 |
+
|
| 102 |
+
my_obj = {
|
| 103 |
+
'form_name': standardize(schedule_name),
|
| 104 |
+
'password': password_1,
|
| 105 |
+
'email': email,
|
| 106 |
+
'capacity': capacity,
|
| 107 |
+
'slots': new_slots,
|
| 108 |
+
'status': 'open',
|
| 109 |
+
'date_created': str(date.today()),
|
| 110 |
+
'responses': json.dumps({
|
| 111 |
+
NAME_COL: [],
|
| 112 |
+
EMAIL_COL: [],
|
| 113 |
+
NUM_WORKSHOPS_COL: [],
|
| 114 |
+
AVAIL_COL: [],
|
| 115 |
+
DESCRIP_COL: [],
|
| 116 |
+
}),
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
client.table('Forms').insert(my_obj).execute()
|
| 120 |
+
gr.Info('', ALERT_TIME, title="Form made successfully!")
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
## Functions to fill out a form @@
|
| 125 |
+
'''
|
| 126 |
+
Gets the timeslots for a given schedule and makes form elements visible.
|
| 127 |
+
Returns:
|
| 128 |
+
gr.Button: corresponds to schedule_name_btn
|
| 129 |
+
gr.CheckboxGroup: corresponds to checkboxes
|
| 130 |
+
gr.Column: corresponds to main_col
|
| 131 |
+
gr.Button: corresponds to submit_preferences_btn
|
| 132 |
+
gr.Textbox: corresponds to new_description
|
| 133 |
+
'''
|
| 134 |
+
def get_timeslots(schedule_name: str):
|
| 135 |
+
# Leave everything as it was
|
| 136 |
+
skip_output = gr.Button(), gr.CheckboxGroup(),gr.Column(), gr.Button(), gr.Textbox()
|
| 137 |
+
|
| 138 |
+
if len(schedule_name) == 0:
|
| 139 |
+
gr.Warning('', ALERT_TIME, title='Please type a form name.')
|
| 140 |
+
return skip_output
|
| 141 |
+
|
| 142 |
+
response = client.table('Forms').select('status', 'slots').eq('form_name', standardize(schedule_name)).execute()
|
| 143 |
+
data = response.data
|
| 144 |
+
|
| 145 |
+
if len(data) > 0:
|
| 146 |
+
my_dict = data[0]
|
| 147 |
+
if my_dict['status'] == 'closed':
|
| 148 |
+
gr.Warning('', ALERT_TIME, title="This form is closed. Please contact the form administrator.")
|
| 149 |
+
return skip_output
|
| 150 |
+
else:
|
| 151 |
+
return gr.Button(variant='secondary'), gr.CheckboxGroup(my_dict['slots'], label="Timeslots", info="Check the time(s) you can teach", visible=True), gr.Column(visible=True), gr.Button(visible=True), gr.Textbox(visible=True)
|
| 152 |
+
else:
|
| 153 |
+
gr.Warning('', ALERT_TIME, title=f"There was no form called \"{schedule_name}\". Please check the spelling and try again.")
|
| 154 |
+
return skip_output
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
'''
|
| 158 |
+
Submits the form that the person filled out to Supabase.
|
| 159 |
+
Returns: None
|
| 160 |
+
'''
|
| 161 |
+
def submit_preferences(schedule_name: str, curr_juggler_name: str, curr_email: str, curr_num_workshops: int, curr_availability: str, curr_descriptions: list):
|
| 162 |
+
# Error handling
|
| 163 |
+
if len(curr_juggler_name) == 0:
|
| 164 |
+
return gr.Warning('', ALERT_TIME, title="Please enter your name.")
|
| 165 |
+
|
| 166 |
+
if len(curr_email) == 0:
|
| 167 |
+
return gr.Warning('', ALERT_TIME, title="Please enter your email address.")
|
| 168 |
+
|
| 169 |
+
if curr_num_workshops == 0:
|
| 170 |
+
return gr.Warning('', ALERT_TIME, title=f"Please enter how many workshops you want to teach.")
|
| 171 |
+
|
| 172 |
+
elif curr_num_workshops < 0:
|
| 173 |
+
return gr.Warning('', ALERT_TIME, title="The number of workshops you want to teach must be positive.")
|
| 174 |
+
|
| 175 |
+
if len(curr_availability) == 0:
|
| 176 |
+
return gr.Warning('', ALERT_TIME, title="Please select at least one timeslot when you are able to teach.")
|
| 177 |
+
|
| 178 |
+
if curr_num_workshops > len(curr_availability):
|
| 179 |
+
return gr.Warning('', ALERT_TIME, title=f"You only selected {len(curr_availability)} timeslots. However, you said you wanted to teach {curr_num_workshops} workshops. Please make sure that you are available to teach during at least {curr_num_workshops} timeslots.")
|
| 180 |
+
|
| 181 |
+
if len(curr_descriptions) == 0:
|
| 182 |
+
return gr.Warning('', ALERT_TIME, title=f"Please describe at least one workshop that you want to teach. You must hit \"Enter\" after each one!")
|
| 183 |
+
|
| 184 |
+
response = client.table('Forms').select('responses').eq('form_name', standardize(schedule_name)).execute()
|
| 185 |
+
data = response.data
|
| 186 |
+
|
| 187 |
+
if len(data) > 0:
|
| 188 |
+
form = json.loads(data[0]['responses'])
|
| 189 |
+
|
| 190 |
+
# Add current preferences to dictionary lists
|
| 191 |
+
curr_juggler_name = curr_juggler_name.strip()
|
| 192 |
+
names = form[NAME_COL]
|
| 193 |
+
if curr_juggler_name in names:
|
| 194 |
+
return gr.Warning('', ALERT_TIME, title=f"Someone already named \"{curr_juggler_name}\" filled out the form. Please use your last name or middle initial.")
|
| 195 |
+
names.extend([curr_juggler_name])
|
| 196 |
+
|
| 197 |
+
emails = form[EMAIL_COL]
|
| 198 |
+
emails.extend([curr_email])
|
| 199 |
+
|
| 200 |
+
bandwidths = form[NUM_WORKSHOPS_COL]
|
| 201 |
+
bandwidths.extend([curr_num_workshops])
|
| 202 |
+
|
| 203 |
+
availabilities = form[AVAIL_COL]
|
| 204 |
+
curr_availability = f"{DELIMITER}".join(curr_availability)
|
| 205 |
+
availabilities.extend([curr_availability])
|
| 206 |
+
|
| 207 |
+
curr_descriptions = [elem['name'] for elem in curr_descriptions]
|
| 208 |
+
curr_descriptions = f"{DELIMITER}".join(curr_descriptions)
|
| 209 |
+
descriptions = form[DESCRIP_COL]
|
| 210 |
+
descriptions.extend([curr_descriptions])
|
| 211 |
+
|
| 212 |
+
# Update Supabase
|
| 213 |
+
my_obj = json.dumps({
|
| 214 |
+
NAME_COL: names,
|
| 215 |
+
EMAIL_COL: emails,
|
| 216 |
+
NUM_WORKSHOPS_COL: bandwidths,
|
| 217 |
+
AVAIL_COL: availabilities,
|
| 218 |
+
DESCRIP_COL: descriptions
|
| 219 |
+
})
|
| 220 |
+
client.table('Forms').update({'responses': my_obj}).eq('form_name', standardize(schedule_name)).execute()
|
| 221 |
+
return gr.Info('', ALERT_TIME, title='Form submitted successfully!')
|
| 222 |
+
|
| 223 |
+
# I don't think it's possible to get here because I checked the schedule name earlier
|
| 224 |
+
else:
|
| 225 |
+
return gr.Warning('', ALERT_TIME, title=f"There was no form called \"{schedule_name}\". Please check the spelling and try again.")
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
## Functions to manage/generate schedules ##
|
| 229 |
+
'''
|
| 230 |
+
Uses the name and password to get the form.
|
| 231 |
+
Makes the buttons and other elements visible on the page.
|
| 232 |
+
Returns:
|
| 233 |
+
gr.Button: corresponds to find_form_btn
|
| 234 |
+
gr.Column: corresponds to all_responses_group
|
| 235 |
+
gr.Column: generate_schedules_explanation
|
| 236 |
+
gr.Row: corresponds to generate_btns
|
| 237 |
+
gr.Column: corresponds to open_close_btn_col
|
| 238 |
+
gr.Button: corresponds to open_close_btn
|
| 239 |
+
'''
|
| 240 |
+
def make_visible(schedule_name:str, password: str):
|
| 241 |
+
skip_output = gr.Button(), gr.Column(), gr.Column(), gr.Row(), gr.Column(), gr.Button()
|
| 242 |
+
|
| 243 |
+
if len(schedule_name) == 0:
|
| 244 |
+
gr.Warning('Please enter the form name.', ALERT_TIME)
|
| 245 |
+
return skip_output
|
| 246 |
+
if len(password) == 0:
|
| 247 |
+
gr.Warning('Please enter the password.', ALERT_TIME)
|
| 248 |
+
return skip_output
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
response = client.table('Forms').select('password', 'status').eq('form_name', standardize(schedule_name)).execute()
|
| 252 |
+
data = response.data
|
| 253 |
+
|
| 254 |
+
if len(data) > 0:
|
| 255 |
+
my_dict = data[0]
|
| 256 |
+
if password != my_dict['password']:
|
| 257 |
+
gr.Warning(INCORRECT_PASSWORD, ALERT_TIME)
|
| 258 |
+
return skip_output
|
| 259 |
+
else:
|
| 260 |
+
if my_dict['status'] == 'open':
|
| 261 |
+
gr.Info('', ALERT_TIME, title='Btw, the form is currently OPEN.')
|
| 262 |
+
return gr.Button(variant='secondary'), gr.Column(visible=True), gr.Column(visible=True), gr.Row(visible=True), gr.Column(visible=True), gr.Button("Close Form", visible=True)
|
| 263 |
+
|
| 264 |
+
elif my_dict['status'] == 'closed':
|
| 265 |
+
gr.Info('', ALERT_TIME, title='Btw, the form is currently CLOSED.')
|
| 266 |
+
return gr.Button(variant='secondary'), gr.Column(visible=True), gr.Column(visible=True), gr.Row(visible=True),gr.Column(visible=True), gr.Button("Open Form", visible=True)
|
| 267 |
+
|
| 268 |
+
else:
|
| 269 |
+
gr.Warning(f"There is no form called \"{schedule_name}\". Please check the spelling and try again.", ALERT_TIME)
|
| 270 |
+
return skip_output
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
'''
|
| 276 |
+
Gets a the form responses from Supabase and converts them to a DataFrame
|
| 277 |
+
Returns:
|
| 278 |
+
if found: a dictionary with two keys, capacity (int) and df (DataFrame)
|
| 279 |
+
if not found: a string indicating the form was not found
|
| 280 |
+
'''
|
| 281 |
+
def get_df_from_db(schedule_name: str, password: str):
|
| 282 |
+
response = client.table('Forms').select('password', 'capacity', 'responses').eq('form_name', standardize(schedule_name)).execute()
|
| 283 |
+
data = response.data
|
| 284 |
+
|
| 285 |
+
if len(data) > 0:
|
| 286 |
+
my_dict = data[0]
|
| 287 |
+
if password != my_dict['password']:
|
| 288 |
+
gr.Warning(INCORRECT_PASSWORD, ALERT_TIME)
|
| 289 |
+
return FORM_NOT_FOUND
|
| 290 |
+
|
| 291 |
+
# Convert to df
|
| 292 |
+
df = pd.DataFrame(json.loads(my_dict['responses']))
|
| 293 |
+
return {'capacity': my_dict['capacity'], 'df': df}
|
| 294 |
+
|
| 295 |
+
else:
|
| 296 |
+
gr.Warning(f"There is no form called \"{schedule_name}\". Please check the spelling and try again.", ALERT_TIME)
|
| 297 |
+
return FORM_NOT_FOUND
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
'''
|
| 301 |
+
Puts all of the form responses into a DataFrame.
|
| 302 |
+
Returns this DF along with the filepath.
|
| 303 |
+
'''
|
| 304 |
+
def get_all_responses(schedule_name:str, password:str):
|
| 305 |
+
res = get_df_from_db(schedule_name, password)
|
| 306 |
+
|
| 307 |
+
if res == FORM_NOT_FOUND:
|
| 308 |
+
df = pd.DataFrame({
|
| 309 |
+
NAME_COL: [],
|
| 310 |
+
EMAIL_COL: [],
|
| 311 |
+
NUM_WORKSHOPS_COL: [],
|
| 312 |
+
AVAIL_COL: [],
|
| 313 |
+
DESCRIP_COL: []
|
| 314 |
+
})
|
| 315 |
+
|
| 316 |
+
else:
|
| 317 |
+
df = res['df']
|
| 318 |
+
# Add spaces
|
| 319 |
+
for col in [AVAIL_COL, DESCRIP_COL]:
|
| 320 |
+
df[col] = [elem.replace(DELIMITER, f"{DELIMITER} ") for elem in df[col].to_list()]
|
| 321 |
+
|
| 322 |
+
directory = os.path.abspath(os.getcwd())
|
| 323 |
+
path = directory + "/schedules/all responses.csv"
|
| 324 |
+
df.to_csv(path, index=False)
|
| 325 |
+
|
| 326 |
+
if len(df) == 0:
|
| 327 |
+
gr.Warning('', ALERT_TIME, title='No one has filled out the form yet.')
|
| 328 |
+
return gr.DataFrame(df, visible=True), gr.File(path, visible=True)
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
'''
|
| 333 |
+
Calls private function to randomly generate 10 of the best schedules.
|
| 334 |
+
Returns: DataFrame, filepath
|
| 335 |
+
'''
|
| 336 |
+
def random_schedules_wrapper(schedule_name: str, password: str):
|
| 337 |
+
gr.Info('', ALERT_TIME, title='Working on generating schedules! Please DO NOT click anything on this page.')
|
| 338 |
+
res = generator(schedule_name, password, api_name='generate_random_schedules')
|
| 339 |
+
df = res[0]['value']
|
| 340 |
+
file = res[1]['value']
|
| 341 |
+
if len(df['data'][0]) == 0:
|
| 342 |
+
gr.Warning('', ALERT_TIME, title='No one has filled out the form yet.')
|
| 343 |
+
else:
|
| 344 |
+
gr.Info('', ALERT_TIME, title='Good news! I was able to make at least one schedule.')
|
| 345 |
+
return gr.DataFrame(df, visible=True), gr.File(file, visible=True)
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
'''
|
| 350 |
+
Calls private function to generate all of the best schedules.
|
| 351 |
+
(The same as random_schedules_wrapper) with a different argument for num_results when calling generator.
|
| 352 |
+
Gradio requires these to be two separate functions.
|
| 353 |
+
Returns: DataFrame, filepath
|
| 354 |
+
'''
|
| 355 |
+
def all_schedules_wrapper(schedule_name: str, password: str):
|
| 356 |
+
gr.Info('', ALERT_TIME, title='Working on generating schedules! Please DO NOT click anything on this page.')
|
| 357 |
+
res = generator(schedule_name, password, api_name='generate_all_schedules')
|
| 358 |
+
df = res[0]['value']
|
| 359 |
+
file = res[1]['value']
|
| 360 |
+
if len(df['data'][0]) == 0:
|
| 361 |
+
gr.Warning('', ALERT_TIME, title='No one has filled out the form yet.')
|
| 362 |
+
else:
|
| 363 |
+
gr.Info('', ALERT_TIME, title='Good news! I was able to make at least one schedule.')
|
| 364 |
+
return gr.DataFrame(df, visible=True), gr.File(file, visible=True)
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
'''
|
| 369 |
+
Opens/closes a form and changes the button after opening/closing the form.
|
| 370 |
+
Returns: gr.Button
|
| 371 |
+
'''
|
| 372 |
+
def toggle_btn(schedule_name:str, password:str):
|
| 373 |
+
response = client.table('Forms').select('password', 'capacity', 'status').eq('form_name', standardize(schedule_name)).execute()
|
| 374 |
+
data = response.data
|
| 375 |
+
|
| 376 |
+
if len(data) > 0:
|
| 377 |
+
my_dict = data[0]
|
| 378 |
+
if password != my_dict['password']:
|
| 379 |
+
gr.Warning(INCORRECT_PASSWORD, ALERT_TIME)
|
| 380 |
+
return FORM_NOT_FOUND
|
| 381 |
+
|
| 382 |
+
curr_status = my_dict['status']
|
| 383 |
+
if curr_status == 'open':
|
| 384 |
+
client.table('Forms').update({'status': 'closed'}).eq('form_name', standardize(schedule_name)).execute()
|
| 385 |
+
gr.Info('', ALERT_TIME, title="The form was closed successfully!")
|
| 386 |
+
return gr.Button('Open Form')
|
| 387 |
+
|
| 388 |
+
elif curr_status == 'closed':
|
| 389 |
+
client.table('Forms').update({'status': 'open'}).eq('form_name', standardize(schedule_name)).execute()
|
| 390 |
+
gr.Info('', ALERT_TIME, title="The form was opened successfully!")
|
| 391 |
+
return gr.Button('Close Form')
|
| 392 |
+
|
| 393 |
+
else:
|
| 394 |
+
gr.Error('', ALERT_TIME, 'An unexpected error has ocurred.')
|
| 395 |
+
return gr.Button()
|
| 396 |
+
|
| 397 |
+
else:
|
| 398 |
+
gr.Warning('', ALERT_TIME, title=f"There was no form called \"{schedule_name}\". Please check the spelling and try again.")
|
| 399 |
+
return gr.Button()
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
### MARKDOWN TEXT ###
|
| 403 |
+
generate_markdown = f"""
|
| 404 |
+
The app will attempt to create schedules where everyone is teaching their descired number of workshops AND all timeslots are filled.\n
|
| 405 |
+
If that is impossible, then the app will create schedules that maximize the number of timeslots that are filled.\n
|
| 406 |
+
You can either get a random selection of the best schedules (recommended), or ALL of the best schedules.\n
|
| 407 |
+
WARNING: It can sometimes take a LONG time to get all the best schedules!
|
| 408 |
+
"""
|
| 409 |
+
|
| 410 |
+
about_markdown = f"""
|
| 411 |
+
# About the App\n
|
| 412 |
+
Hi! My name is Logan, and I created Schedule Buddy to be the one-stop-shop for making juggling workshop schedules.\n
|
| 413 |
+
Making a juggling workshop schedule involves 3 parts: making the form, having people fill it out, and putting the schedule together. Schedule Buddy supports all three of these aspects!\n
|
| 414 |
+
|
| 415 |
+
Schedule Buddy streamlines the process of the creating and filling out the forms, essentially replacing Google Forms.
|
| 416 |
+
In terms of putting the schedule togther, Schedule Buddy will attempt to create schedules where everyone is teaching their desired number of workshops AND all timeslots are filled.
|
| 417 |
+
If that is impossible, then the app will create schedules that maximize the number of workshops that are taught.
|
| 418 |
+
Essentially, Schedule Buddy removes the headache of trying to fit everyone into a timeslot. \n
|
| 419 |
+
For those who are curious and still reading, Schedule Buddy uses a recursive backtracking algorithm to make schedules.
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
# About Me
|
| 423 |
+
I've been juggling for the past 8 years, and 4 years ago I created a YouTube channel called Juggling Gym.\n
|
| 424 |
+
I love going to juggling festivals and attending workshops. When I was planning the workshops for the Atlanta Juggling Festival, I noticed how hard it was to plan the workshops
|
| 425 |
+
and make sure that everyone was teaching their desired number of workshops.\n
|
| 426 |
+
Since workshops are entirely run by volunteers, I wanted to make the process easier for everyone! Thus, I created Schedule Buddy as a free resource for jugglers to plan workshops.\n
|
| 427 |
+
"""
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
### GRADIO ###
|
| 431 |
+
with gr.Blocks() as demo:
|
| 432 |
+
### FILL OUT FORM ###
|
| 433 |
+
with gr.Tab('Fill Out Form'):
|
| 434 |
+
schedule_name = gr.Textbox(label="Form Name", info="What is the name of the form you want to fill out?")
|
| 435 |
+
schedule_name_btn = gr.Button('Submit', variant='primary')
|
| 436 |
+
|
| 437 |
+
with gr.Column(visible=False) as main_col:
|
| 438 |
+
juggler_name = gr.Textbox(label='Name (first and last)', visible=True)
|
| 439 |
+
email = gr.Textbox(label='Email Address', visible=True)
|
| 440 |
+
num_workshops = gr.Number(label="Number of Workshops", info="Enter how many workshops you want to teach, e.g., \"1\", \"2\", etc.", interactive=True, visible=True)
|
| 441 |
+
|
| 442 |
+
checkboxes = gr.CheckboxGroup([], label="Timeslots", info="Check the time(s) you can teach.", visible=False)
|
| 443 |
+
|
| 444 |
+
# Let the user dynamically describe their workshops
|
| 445 |
+
descriptions = gr.State([])
|
| 446 |
+
new_description = gr.Textbox(label='Workshop Descriptions', info='Describe the workshop(s) you want to teach. Hit "Enter" after each one.', visible=False)
|
| 447 |
+
|
| 448 |
+
def add_descrip(descriptions, new_description):
|
| 449 |
+
return descriptions + [{"name": new_description}], ""
|
| 450 |
+
|
| 451 |
+
new_description.submit(add_descrip, [descriptions, new_description], [descriptions, new_description])
|
| 452 |
+
|
| 453 |
+
@gr.render(inputs=descriptions)
|
| 454 |
+
def render_descriptions(descrip_list):
|
| 455 |
+
for elem in descrip_list:
|
| 456 |
+
with gr.Row():
|
| 457 |
+
gr.Textbox(elem['name'], show_label=False, container=False)
|
| 458 |
+
delete_btn = gr.Button("Delete", scale=0, variant="stop")
|
| 459 |
+
def delete(elem=elem):
|
| 460 |
+
descrip_list.remove(elem)
|
| 461 |
+
return descrip_list
|
| 462 |
+
delete_btn.click(delete, None, [descriptions])
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
submit_preferences_btn = gr.Button('Submit', variant='primary', visible=False)
|
| 466 |
+
schedule_name_btn.click(fn=get_timeslots, inputs=[schedule_name], outputs=[schedule_name_btn, checkboxes, main_col, submit_preferences_btn, new_description])
|
| 467 |
+
submit_preferences_btn.click(fn=submit_preferences, inputs=[schedule_name, juggler_name, email, num_workshops, checkboxes, descriptions])
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
### MAKE FORM ###
|
| 471 |
+
with gr.Tab('Make Form'):
|
| 472 |
+
email = gr.Textbox(label="Email Adress")
|
| 473 |
+
schedule_name = gr.Textbox(label="Form Name", info='Keep it simple! Each person will have to type the form name to fill it out.')
|
| 474 |
+
password_1 = gr.Textbox(label='Password', info='You MUST remember your password to access the schedule results. There is currently no way to reset your password.')
|
| 475 |
+
password_2 = gr.Textbox(label='Password Again', info='Enter your password again')
|
| 476 |
+
capacity = gr.Number(label="Capacity", info="Enter the maximum number of people who can teach per timeslot.")
|
| 477 |
+
|
| 478 |
+
# Dynamically render timeslots
|
| 479 |
+
# Based on: https://www.gradio.app/guides/dynamic-apps-with-render-decorator
|
| 480 |
+
slots = gr.State([])
|
| 481 |
+
new_slot = gr.Textbox(label='Enter Timeslots People Can Teach', info='Ex: Friday 7 pm, Saturday 11 am. Hit "Enter" after each one. Make sure to put them in CHRONOLOGICAL ORDER!')
|
| 482 |
+
|
| 483 |
+
def add_slot(slots, new_slot_name):
|
| 484 |
+
return slots + [{"name": new_slot_name}], ""
|
| 485 |
+
|
| 486 |
+
new_slot.submit(add_slot, [slots, new_slot], [slots, new_slot])
|
| 487 |
+
|
| 488 |
+
@gr.render(inputs=slots)
|
| 489 |
+
def render_slots(slot_list):
|
| 490 |
+
gr.Markdown(f"### Timeslots")
|
| 491 |
+
for slot in slot_list:
|
| 492 |
+
with gr.Row():
|
| 493 |
+
gr.Textbox(slot['name'], show_label=False, container=False)
|
| 494 |
+
delete_btn = gr.Button("Delete", scale=0, variant="stop")
|
| 495 |
+
def delete(slot=slot):
|
| 496 |
+
slot_list.remove(slot)
|
| 497 |
+
return slot_list
|
| 498 |
+
delete_btn.click(delete, None, [slots])
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
btn = gr.Button('Submit', variant='primary')
|
| 502 |
+
btn.click(
|
| 503 |
+
fn=make_form,
|
| 504 |
+
inputs=[email, schedule_name, password_1, password_2, capacity, slots],
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
### VIEW FORM RESULTS ###
|
| 509 |
+
with gr.Tab('View Form Results'):
|
| 510 |
+
with gr.Column() as btn_group:
|
| 511 |
+
schedule_name = gr.Textbox(label="Form Name")
|
| 512 |
+
password = gr.Textbox(label="Password")
|
| 513 |
+
find_form_btn = gr.Button('Find Form', variant='primary')
|
| 514 |
+
|
| 515 |
+
# 1. Get all responses
|
| 516 |
+
with gr.Column(visible=False) as all_responses_col:
|
| 517 |
+
gr.Markdown('# Download All Form Responses')
|
| 518 |
+
gr.Markdown("Download everyone's responses to the form.")
|
| 519 |
+
all_responses_btn = gr.Button('Download All Form Responses', variant='primary')
|
| 520 |
+
|
| 521 |
+
with gr.Row() as all_responses_output_row:
|
| 522 |
+
df_out = gr.DataFrame(row_count = (NUM_ROWS, "dynamic"),col_count = (NUM_COLS_ALL_RESPONSES, "dynamic"),headers=[NAME_COL, NUM_WORKSHOPS_COL, AVAIL_COL, DESCRIP_COL],wrap=True,scale=4,visible=False)
|
| 523 |
+
file_out = gr.File(label = "Downloadable file", scale=1, visible=False)
|
| 524 |
+
|
| 525 |
+
all_responses_btn.click(fn=get_all_responses, inputs=[schedule_name, password], outputs=[df_out, file_out])
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
# 2. Generate schedules
|
| 529 |
+
with gr.Column(visible=False) as generate_schedules_explanation_col:
|
| 530 |
+
gr.Markdown('# Create Schedules based on Everyone\'s Preferences.')
|
| 531 |
+
with gr.Accordion('Details'):
|
| 532 |
+
gr.Markdown(generate_markdown)
|
| 533 |
+
|
| 534 |
+
with gr.Row(visible=False) as generate_btns_row:
|
| 535 |
+
generate_ten_results_btn = gr.Button('Generate a Subset of Schedules', variant='primary', visible=True)
|
| 536 |
+
generate_all_results_btn = gr.Button('Generate All Possible Schedules', visible=True)
|
| 537 |
+
|
| 538 |
+
with gr.Row(visible=True) as generated_schedules_output:
|
| 539 |
+
generated_df_out = gr.DataFrame(row_count = (NUM_ROWS, "dynamic"),col_count = (NUM_COLS_SCHEDULES, "dynamic"),headers=["Schedule", "Instructors"],wrap=True,scale=3, visible=False)
|
| 540 |
+
generated_file_out = gr.File(label = "Downloadable schedule file", scale=1, visible=False)
|
| 541 |
+
|
| 542 |
+
generate_ten_results_btn.click(fn=random_schedules_wrapper, inputs=[schedule_name, password], outputs=[generated_df_out, generated_file_out])
|
| 543 |
+
generate_all_results_btn.click(fn=all_schedules_wrapper, inputs=[schedule_name, password], outputs=[generated_df_out, generated_file_out], api_name='generate_all_schedules')
|
| 544 |
+
|
| 545 |
+
|
| 546 |
+
# 3. Open/close button
|
| 547 |
+
with gr.Column(visible=False) as open_close_btn_col:
|
| 548 |
+
gr.Markdown('# Open or Close Form')
|
| 549 |
+
open_close_btn = gr.Button(variant='primary')
|
| 550 |
+
open_close_btn.click(fn=toggle_btn, inputs=[schedule_name, password], outputs=[open_close_btn])
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
find_form_btn.click(fn=make_visible, inputs=[schedule_name, password], outputs=[find_form_btn, all_responses_col, generate_schedules_explanation_col, generate_btns_row, open_close_btn_col, open_close_btn])
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
### INFO ###
|
| 557 |
+
with gr.Tab('About'):
|
| 558 |
+
gr.Markdown(about_markdown)
|
| 559 |
+
|
| 560 |
+
directory = os.path.abspath(os.getcwd())
|
| 561 |
+
allowed = directory + "/schedules"
|
| 562 |
+
demo.launch(allowed_paths=[allowed])
|