Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import yfinance as yf
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
from data import all_historical_data
|
| 8 |
+
from config import tickers_to_ids
|
| 9 |
+
from processes import evolucion_correlacion, plot_evolucion_correlacion_gradio
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# -----------------------------------------------
|
| 13 |
+
# CONFIGURACIÓN DE LA APP CON GRADIO
|
| 14 |
+
# -----------------------------------------------
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
with gr.Blocks(title="Análisis de Correlación") as demo:
|
| 18 |
+
gr.Image("https://cdn.prod.website-files.com/67174dcb1d43b06a0eae317e/672bbbc7a2b88045e5661994_logo-afi.svg", show_label=False)
|
| 19 |
+
gr.Markdown("# Analizador de Correlación de Bitcoin")
|
| 20 |
+
gr.Markdown("Explora la correlación de diferentes activos con Bitcoin y sus holdings.")
|
| 21 |
+
|
| 22 |
+
with gr.Row():
|
| 23 |
+
input_company = gr.Dropdown(
|
| 24 |
+
label="Buscar por Nombre (se usarán los primeros 6 caracteres)",
|
| 25 |
+
choices=tickers,
|
| 26 |
+
value="MicroStrategy",
|
| 27 |
+
allow_custom_value=True,
|
| 28 |
+
interactive=True
|
| 29 |
+
)
|
| 30 |
+
texto_info1 = gr.Textbox(label="Nombre Completo:", interactive=False)
|
| 31 |
+
# ESTE COMPONENTE ALMACENARÁ NUESTRO ESTADO (EL TICKER)
|
| 32 |
+
texto_info2 = gr.Textbox(label="Ticker:", interactive=True)
|
| 33 |
+
|
| 34 |
+
input_company.change(
|
| 35 |
+
fn=lambda texto_completo: find_company_online(texto_completo[:6]),
|
| 36 |
+
inputs=input_company,
|
| 37 |
+
outputs=[texto_info1, texto_info2]
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Es buena práctica ejecutar la función al cargar para rellenar los valores iniciales
|
| 41 |
+
demo.load(
|
| 42 |
+
fn=lambda texto_completo: find_company_online(texto_completo[:6]),
|
| 43 |
+
inputs=input_company,
|
| 44 |
+
outputs=[texto_info1, texto_info2]
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with gr.Tab("Evolución de Correlación"):
|
| 48 |
+
with gr.Row():
|
| 49 |
+
input_step = gr.Slider(minimum=1, maximum=30, value=1, step=1, label="Indica cada cuántos días se calcula la correlación")
|
| 50 |
+
input_window = gr.Slider(minimum=1, maximum=60, value=12, step=1, label="Ventana rolling (meses)")
|
| 51 |
+
btn_plot = gr.Button("Mostrar Gráfico")
|
| 52 |
+
output_plot = gr.Plot(label="Gráfico de Evolución")
|
| 53 |
+
btn_excel = gr.Button("Descargar en Excel")
|
| 54 |
+
output_file = gr.File(label="Archivo Excel")
|
| 55 |
+
output_msg = gr.Label()
|
| 56 |
+
|
| 57 |
+
btn_plot.click(
|
| 58 |
+
fn=plot_evolucion_correlacion_gradio,
|
| 59 |
+
inputs=[texto_info2, input_step, input_window],
|
| 60 |
+
outputs=[output_plot, output_msg]
|
| 61 |
+
)
|
| 62 |
+
btn_excel.click(
|
| 63 |
+
fn=exportar_correlacion,
|
| 64 |
+
inputs=[texto_info2, input_step, input_window],
|
| 65 |
+
outputs=[output_file, output_msg]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
with gr.Tab("Stock vs BTC"):
|
| 69 |
+
|
| 70 |
+
btn_stock_btc = gr.Button("Cargar Gráfico")
|
| 71 |
+
output_iframe_stock = gr.HTML(label="Stock vs Bitcoin Chart")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
btn_stock_btc.click(
|
| 75 |
+
fn=get_iframe_stock_vs_btc,
|
| 76 |
+
inputs=[texto_info2],
|
| 77 |
+
outputs=[output_iframe_stock]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with gr.Tab("Holding de Bitcoin"):
|
| 81 |
+
|
| 82 |
+
btn_holdings = gr.Button("Cargar Gráfico")
|
| 83 |
+
output_iframe_holdings = gr.HTML(label="Bitcoin Holdings Chart")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
btn_holdings.click(
|
| 87 |
+
fn=get_iframe_holdings,
|
| 88 |
+
inputs=[texto_info2],
|
| 89 |
+
outputs=[output_iframe_holdings]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Tab("Exportar Resumen Holding"):
|
| 93 |
+
|
| 94 |
+
input_template_url = gr.Textbox(
|
| 95 |
+
label="URL de la plantilla de Excel (Google Drive)",
|
| 96 |
+
value="https://docs.google.com/spreadsheets/d/1Ad3bbYQAAZO6ljkaCGTGSw5aG_FQF7Lg/edit?usp=sharing",
|
| 97 |
+
placeholder="Ej: https://docs.google.com/spreadsheets/d/ID_DEL_ARCHIVO/..."
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column():
|
| 102 |
+
gr.Markdown("### Top Holding ETFs")
|
| 103 |
+
gr.HTML(value=styled_ETFs.to_html())
|
| 104 |
+
with gr.Column():
|
| 105 |
+
gr.Markdown("### Top Holding Stocks")
|
| 106 |
+
gr.HTML(value=styled_stocks.to_html())
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
btn_descargar_resumen = gr.Button("Descargar Resumen en Excel")
|
| 110 |
+
output_resumen_file = gr.File(label="Archivo de Resumen de Holdings")
|
| 111 |
+
output_resumen_msg = gr.Label()
|
| 112 |
+
|
| 113 |
+
btn_descargar_resumen.click(
|
| 114 |
+
fn=descargar_resumen_holding_excel,
|
| 115 |
+
inputs=[input_template_url],
|
| 116 |
+
outputs=[output_resumen_file, output_resumen_msg]
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Inicia la aplicación
|
| 120 |
+
demo.launch(share=True)
|