| import requests | |
| from datetime import datetime | |
| import pandas as pd | |
| import gradio as gr | |
| def make_appointment(): | |
| url = "http://localhost:8000/appointments/available" | |
| available_dates = requests.get(url) | |
| available_days = list(available_dates.json()['available_dates'].keys()) | |
| update_home_tab = gr.update(visible=False) | |
| update_schedule_tab = gr.update(visible=True) | |
| update_search_tab = gr.update(visible=False) | |
| update_available_days = gr.update( | |
| choices=available_days, value=available_days[0], allow_custom_value=True, interactive=True | |
| ) | |
| update_hours = get_available_hours(available_days[0], available_dates) | |
| return update_home_tab, update_schedule_tab, update_search_tab, available_dates, update_available_days, update_hours | |
| def search_appointments_days(): | |
| url = "http://localhost:8000/appointments/reserved" | |
| reserved_days = requests.get(url) | |
| reserved_days = list(reserved_days.json()['reserved_days']) | |
| update_home_tab = gr.update(visible=False) | |
| update_schedule_tab = gr.update(visible=False) | |
| update_search_tab = gr.update(visible=True) | |
| update_reserved_days = gr.update( | |
| choices=reserved_days, value=reserved_days[0], allow_custom_value=True, interactive=True | |
| ) | |
| return update_home_tab, update_schedule_tab, update_search_tab, update_reserved_days | |
| def get_back_home(): | |
| update_home_tab = gr.update(visible=True) | |
| update_schedule_tab = gr.update(visible=False) | |
| update_search_tab = gr.update(visible=False) | |
| return update_home_tab, update_schedule_tab, update_search_tab | |
| def _get_long_hour(hours): | |
| formatted_hours = [ | |
| datetime.strptime(hour, '%H:%M').strftime('%I:%M %p').replace("AM", "a.m.").replace("PM", "p.m.") | |
| for hour in hours | |
| ] | |
| return formatted_hours | |
| def _get_short_hour(hour): | |
| return datetime.strptime(hour.replace("a.m.", "AM").replace("p.m.", "PM"), '%I:%M %p').strftime('%H:%M') | |
| def get_available_hours(selected_day, available_dates): | |
| available_hours = available_dates.json()['available_dates'][selected_day] | |
| formatted_hours = _get_long_hour(available_hours) | |
| update_hours = gr.update(choices=formatted_hours, value=formatted_hours[0], allow_custom_value=True, interactive=True) | |
| return update_hours | |
| def create_appointment(identification, name, last_name, age, day, hour): | |
| url = "http://localhost:8000/appointments/" | |
| data = { | |
| "user": { | |
| "identification": identification, | |
| "name": name, | |
| "last_name": last_name, | |
| "age": age | |
| }, | |
| "full_date": { | |
| "date": day, | |
| "hour": _get_short_hour(hour) | |
| } | |
| } | |
| response = requests.post(url, json=data) | |
| if response.status_code != 200: | |
| update_home_tab, update_schedule_tab, update_search_tab = gr.update(), gr.update(), gr.update() | |
| gr.Warning(f"Error: {response.json()}") | |
| return update_home_tab, update_schedule_tab, update_search_tab | |
| update_home_tab = gr.update(visible=True) | |
| update_schedule_tab = gr.update(visible=False) | |
| update_search_tab = gr.update(visible=False) | |
| gr.Info("Cita agendada con éxito") | |
| return update_home_tab, update_schedule_tab, update_search_tab | |
| def search_appointments(selected_day): | |
| url = f"http://localhost:8000/appointments/{selected_day}" | |
| appointments = requests.get(url) | |
| appointments = appointments.json()['appointments'] | |
| df = pd.DataFrame(data=appointments) | |
| df = df.rename(columns={ | |
| 'hour': 'Hora', 'identification': 'Cedula', 'name': 'Nombres' | |
| }) | |
| update_appointments = gr.update(value=df) | |
| return update_appointments |