Spaces:
Configuration error
Configuration error
| import streamlit as st | |
| from plotly import graph_objs as go | |
| import pandas as pd | |
| from pandas.core.groupby.groupby import DataError | |
| from pytrends.request import TrendReq | |
| from datetime import datetime, timedelta, date | |
| import numpy as np | |
| from plotly.subplots import make_subplots | |
| from metodos import colores_corporativos | |
| import pybase64 as base64 | |
| import io | |
| from logs_portal import log | |
| from Scheduler import Scheduler_Covid as sc | |
| import os | |
| def button_style(): | |
| style_button = """ | |
| <style> | |
| button { | |
| margin-top:-100px; | |
| display: inline-block; | |
| background-color: #e8e8e8; | |
| border-radius: 15px; | |
| border: 4px #cccccc; | |
| color: #4a4a4a; | |
| text-align: center; | |
| font-size: 15px; | |
| padding: 2px; | |
| width: 260px; | |
| transition: all 0.5s; | |
| cursor: pointer; | |
| margin: 5px; | |
| } | |
| button span { | |
| cursor: pointer; | |
| display: inline-block; | |
| position: relative; | |
| transition: 0.5s; | |
| } | |
| button span:after { | |
| content: '\00bb'; | |
| position: absolute; | |
| opacity: 0; | |
| top: 0; | |
| right: -20px; | |
| transition: 0.5s; | |
| } | |
| button:hover { | |
| background-color: #bb1114; | |
| color:#e8e8e8; | |
| } | |
| button:hover span { | |
| padding-right: 25px; | |
| } | |
| button:hover span:after { | |
| opacity: 1; | |
| right: 0; | |
| } | |
| </style> | |
| """ | |
| st.markdown(style_button, unsafe_allow_html=True) | |
| def get_table_download_link(df): | |
| """Generates a link allowing the data in a given panda dataframe to be | |
| downloaded | |
| in: dataframe | |
| out: href string | |
| """ | |
| csv = df.to_csv(index=False) | |
| b64 = base64.b64encode(csv.encode()).decode() | |
| name_arch = "Scoring_filtrado.csv" | |
| name_mark = "Descargar .csv " | |
| style = '"color:black;text-decoration: none;font-size:18px;"' | |
| href = f'<center><a href="data:file/csv;base64,{b64}" style=' + style+' download="'+name_arch+'" ><button>'+name_mark+'</button></a></center>' | |
| return href | |
| def get_table_excel_link(df, name_arch): | |
| towrite = io.BytesIO() | |
| downloaded_file = df.to_excel(towrite, encoding='utf-8', index=False, | |
| header=True) | |
| towrite.seek(0) # reset pointer | |
| file_name = name_arch | |
| style = 'style="color:black;text-decoration: none; font-size:18px;" ' | |
| name_mark = "Descargar "+name_arch | |
| b64 = base64.b64encode(towrite.read()).decode() # some strings | |
| linko= f'<center><a href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}" '+style+'download="'+file_name+'"><button>'+name_mark+'</button></a></center>' | |
| return linko | |
| def charged_data(): | |
| regiones = {} | |
| regiones['Latam'] = ['Argentina', 'Brazil', 'Chile', 'Colombia', | |
| 'Mexico', 'Peru'] | |
| regiones['Europa'] = ['Italy', 'Spain', 'Germany', 'United Kingdom', | |
| 'France'] | |
| regiones['Asia Emergente'] = ['South Korea', 'Taiwan', 'Hong Kong', | |
| 'India', 'Thailand', 'Indonesia'] | |
| regiones['USA'] = ['United States'] | |
| data_dict = np.load('Scheduler/dict_movilidad.npy', | |
| allow_pickle='TRUE').item() | |
| return data_dict, regiones | |
| def charged_data2(): | |
| covid_data = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv') | |
| paises = {'CL': 'Chile', 'AR': 'Argentina', 'BR': 'Brazil', | |
| 'MX': 'Mexico'} | |
| covid_data = covid_data.loc[covid_data['location'].isin(paises.values())] | |
| covid_data['date'] = pd.to_datetime(covid_data['date']) | |
| covid_data.set_index(['date', 'location'], inplace=True) | |
| # Creamos diccionario con cada una de las variables para distintos países | |
| data_dict = {} | |
| for col in covid_data.columns: | |
| try: | |
| data_dict[col] = covid_data[col].unstack().fillna(0).rolling(1).mean() | |
| except DataError: | |
| pass | |
| # Descargamos la data de google trends | |
| pytrends = TrendReq(retries=5, backoff_factor=0.2, | |
| requests_args={'verify': False}) | |
| start = (datetime.today() - timedelta(180)).strftime("%Y-%m-%d") | |
| start = datetime(2020, 2, 1).strftime("%Y-%m-%d") | |
| end = datetime.today().strftime("%Y-%m-%d") | |
| tf = f'{start} {end}' | |
| kw_lists = { | |
| 'CL': ['PCR', 'sintomas covid', 'examen covid', | |
| 'covid positivo'], | |
| 'AR': ['PCR', 'olfato', 'sintomas covid', 'perdida gusto', | |
| 'covid positivo'], | |
| 'MX': ['PCR', 'olfato', 'sintomas covid', 'covid positivo', | |
| 'perdida gusto'], | |
| 'BR': ['PCR', 'sintomas covid', 'exame covid', 'covid positivo'] | |
| } | |
| gt_data = {} | |
| for p, kw in kw_lists.items(): | |
| pytrends.build_payload(kw, timeframe=tf, geo=p) | |
| df = pytrends.interest_over_time().drop(columns='isPartial') | |
| gt_data[paises[p]] = df.div(df.mean(0).values) | |
| data_dict['GT Index'] = pd.DataFrame({p: gt_data[p].mean(1).rolling(1).mean() | |
| for p in gt_data.keys()}) | |
| return data_dict, paises | |
| def Movilidad(): | |
| largo = 400 | |
| ancho = 550 | |
| button_style() | |
| placebar = st.empty() | |
| percent_complete = 0 | |
| my_bar = placebar.progress(percent_complete) | |
| data_cargada = charged_data() | |
| data_dict = data_cargada[0] | |
| regiones = data_cargada[1] | |
| europa = data_dict['Mobility Index'][regiones.keys()]["Europa"] | |
| latam = data_dict['Mobility Index'][regiones.keys()]["Latam"] | |
| asia = data_dict['Mobility Index'][regiones.keys()]["Asia Emergente"] | |
| USA = data_dict['Mobility Index'][regiones.keys()]["USA"] | |
| mov_region = data_dict['Mobility Index'][regiones.keys()][["USA", "Europa","Asia Emergente", "Latam"]] | |
| percent_complete = percent_complete+33 | |
| placebar.progress(percent_complete) | |
| colores = list(colores_corporativos().values()) | |
| colores2 = [] | |
| for i in range(len(colores)): | |
| colores2.append("rgb"+str(colores[i])) | |
| def plot_raw_data(): | |
| fig = go.Figure() | |
| europa_ = go.Scatter(x=europa.index, y=europa.values, name="Europa", | |
| line=dict(color=colores2[0])) | |
| latam_ = go.Scatter(x=latam.index, y=latam.values, name="Latam", | |
| line=dict(color=colores2[1])) | |
| USA_ = go.Scatter(x=USA.index, y=USA.values, name="USA", | |
| line=dict(color=colores2[2])) | |
| asia_ = go.Scatter(x=asia.index, y=asia.values, name="Asia Emergente", | |
| line=dict(color=colores2[3])) | |
| fig.add_trace(europa_) | |
| fig.add_trace(latam_) | |
| fig.add_trace(USA_) | |
| fig.add_trace(asia_) | |
| fig.layout.update(title_text="Evolución por region", | |
| xaxis_rangeslider_visible=True, | |
| margin_b=20, | |
| margin_r=20, | |
| margin_l=20, | |
| width=ancho, | |
| height=largo, | |
| legend=dict(orientation="h", | |
| yanchor="bottom", | |
| y=1.02, | |
| xanchor="right", | |
| x=1)) | |
| fig2 = go.Figure() | |
| i = 0 | |
| for pais in regiones["Latam"]: | |
| data_pais = data_dict['Mobility Index'][regiones['Latam']][pais] | |
| pais_gr = go.Scatter(x=data_pais.index, | |
| y=data_pais.values, name=pais, | |
| line=dict(color=colores2[i])) | |
| fig2.add_trace(pais_gr) | |
| i = i+1 | |
| fig2.layout.update(title_text="Evolución LATAM", | |
| xaxis_rangeslider_visible=True, margin_b=20, | |
| margin_r=20,margin_l=20, | |
| width=ancho, height=largo, | |
| legend=dict(orientation="h", | |
| yanchor="bottom", | |
| y=1.0, | |
| xanchor="right", | |
| x=1)) | |
| col1, col2 = st.columns(2) | |
| col1.plotly_chart(fig, use_container_width=True) | |
| col2.plotly_chart(fig2, use_container_width=True) | |
| link_excel_1 = get_table_excel_link(data_dict['Mobility Index'][regiones['Latam']], "Movilidad Latam.xlsx") | |
| link_excel_2 = get_table_excel_link(mov_region, "Movilidad por region.xlsx") | |
| col1.markdown(link_excel_1, unsafe_allow_html=True) | |
| col2.markdown(link_excel_2, unsafe_allow_html=True) | |
| percent_complete = percent_complete + 33 | |
| placebar.progress(percent_complete) | |
| placebar.empty() | |
| plot_raw_data() | |
| percent_complete = percent_complete + 34 | |
| my_bar.progress(percent_complete) | |
| my_bar.empty() | |
| data_desag = pd.read_excel("Scheduler/Movilidad_desagrada.xlsx", | |
| engine="openpyxl") | |
| st.markdown(get_table_excel_link(data_desag, "Movilidad desagregada.xlsx"), | |
| unsafe_allow_html=True) | |
| try: | |
| user = os.getlogin() | |
| if user == 'bullm': | |
| act = st.button('Actualizar') | |
| if act: | |
| sc.run_data_covid() | |
| ud = pd.read_excel('Data/update_data.xlsx') | |
| ud = ud[ud['View'] != 'Covid19'] | |
| today = date.today().strftime('%d-%m-%Y') | |
| ud = ud.append({"View": "Covid19", | |
| "Last_Update": today}, ignore_index=True) | |
| ud.to_excel('Data/update_data.xlsx', index=False) | |
| except Exception: | |
| pass | |
| def Correlacion_GT(): | |
| largo = 400 | |
| ancho = 550 | |
| button_style() | |
| # Cargamos la data relevante | |
| percent_complete = 0 | |
| my_bar = st.progress(percent_complete) | |
| percent_complete = percent_complete + 33 | |
| my_bar.progress(percent_complete) | |
| data_cargada = charged_data2() | |
| data_dict = data_cargada[0] | |
| paises = data_cargada[1] | |
| corr_df = pd.DataFrame(index=paises.values(), columns=np.arange(-3, 1)) | |
| percent_complete = percent_complete + 33 | |
| my_bar.progress(percent_complete) | |
| i = 0 | |
| cols = st.columns(2) | |
| col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7)) | |
| for p in corr_df.index: | |
| df = pd.concat([data_dict['GT Index'][p], | |
| data_dict['new_cases_per_million'][p]], | |
| axis=1).dropna() | |
| df.columns = ['GT Index', 'Nuevos Casos Confirmados'] | |
| fig = make_subplots(specs=[[{"secondary_y": True}]]) | |
| CC = go.Scatter(x=df['GT Index'].index, | |
| y=df['GT Index'].values, name='GT index', | |
| line=dict(color='dimgrey')) | |
| GT = go.Scatter(x=df['Nuevos Casos Confirmados'].index, | |
| y=df['Nuevos Casos Confirmados'].values, | |
| name='Casos confirmados', line=dict(color='darkred')) | |
| fig.add_trace(CC, secondary_y=False,) | |
| fig.add_trace(GT, secondary_y=True,) | |
| fig.layout.update(title_text="Evolución {}".format(p), | |
| xaxis_rangeslider_visible=True, margin_b=20, | |
| margin_r=20, margin_l=20, | |
| width=ancho, height=largo, | |
| legend=dict(orientation="h", | |
| yanchor="bottom", | |
| y=1.02, | |
| xanchor="right", | |
| x=1)) | |
| link_excel = get_table_excel_link(df, "Correlacion GT.xlsx") | |
| if i % 2 == 0: | |
| cols[0].plotly_chart(fig, use_container_width=True) | |
| cols[0].markdown(link_excel, unsafe_allow_html=True) | |
| else: | |
| cols[1].plotly_chart(fig, use_container_width=True) | |
| cols[1].markdown(link_excel, unsafe_allow_html=True) | |
| cols = st.columns(2) | |
| col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7)) | |
| i = i + 1 | |
| percent_complete = percent_complete + 34 | |
| my_bar.progress(percent_complete) | |
| my_bar.empty() | |
| def vacunas(): | |
| largo = 400 | |
| ancho = 550 | |
| button_style() | |
| vac_data = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv').set_index(['date','location']) | |
| country_pop = (vac_data['population'].reset_index().set_index('location') | |
| .drop(columns='date').squeeze().drop_duplicates()) | |
| min_pop = 1000000 | |
| idx = country_pop[country_pop > min_pop].index | |
| vac_data = vac_data['total_vaccinations_per_hundred'].unstack().ffill().fillna(0) | |
| vac_data.index = pd.to_datetime(vac_data.index) | |
| N = 15 | |
| top_vac = vac_data[idx].iloc[-1].nlargest(N).sort_values() | |
| regiones = {} | |
| regiones['Latam'] = ['Argentina', 'Brazil', 'Chile', 'Colombia', | |
| 'Mexico', 'Peru'] | |
| regiones['Europa'] = ['Italy', 'Spain', 'Germany', 'United Kingdom', | |
| 'France', 'Russia'] | |
| regiones['Asia Emergente'] = ['South Korea', 'Taiwan', 'Hong Kong', | |
| 'China', 'Japan'] | |
| regiones['Norteamérica'] = ['United States', 'Canada'] | |
| inicio = datetime(2020, 11, 15) | |
| vac_data = vac_data.loc[vac_data.index > inicio].resample('W').last() | |
| vac_data.index.name = '' | |
| colores = colores_corporativos().values() | |
| colores = list(colores_corporativos().values()) | |
| colores2 = [] | |
| for i in range(len(colores)): | |
| colores2.append("rgb"+str(colores[i])) | |
| def plot_raw_data(): | |
| i = 0 | |
| cols = st.columns(2) | |
| col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7)) | |
| for region in list(regiones.keys()): | |
| fig = go.Figure() | |
| j = 0 | |
| for pais in regiones[region]: | |
| data_pais = vac_data[regiones[region]][pais] | |
| pais_gr = go.Scatter(x=data_pais.index, | |
| y=data_pais.values, name=pais, | |
| line=dict(color=colores2[j])) | |
| fig.add_trace(pais_gr) | |
| j = j+1 | |
| fig.layout.update(title_text="Evolución "+region, | |
| xaxis_rangeslider_visible=True, height=largo, | |
| width=ancho, margin_b=20, | |
| legend=dict(orientation="h", | |
| yanchor="bottom", | |
| y=1.0, | |
| xanchor="right", | |
| x=1)) | |
| link_excel = get_table_excel_link(data_pais, "Vacunacion.xlsx") | |
| if i % 2 == 0: | |
| cols[0].plotly_chart(fig, use_column_width=True) | |
| cols[0].markdown(link_excel, unsafe_allow_html=True) | |
| else: | |
| cols[1].plotly_chart(fig, use_column_width=True) | |
| cols[1].markdown(link_excel, unsafe_allow_html=True) | |
| cols = st.columns(2) | |
| col1, col2, col3, col4 = st.columns((1.5, 7, 2, 7)) | |
| i = i+1 | |
| plot_raw_data() | |