Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app_npv_timeline.py
|
| 2 |
+
# -------------------------------------------------------------
|
| 3 |
+
# Herramienta interactiva para calcular el Valor Presente Neto
|
| 4 |
+
# (NPV) de un proyecto a partir de flujos de caja anuales.
|
| 5 |
+
# -------------------------------------------------------------
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# 1. CONFIGURACIÓN BÁSICA DE LA PÁGINA
|
| 11 |
+
st.set_page_config(page_title="Calculadora de NPV", layout="wide")
|
| 12 |
+
|
| 13 |
+
# 2. ENCABEZADO
|
| 14 |
+
st.title("Calculadora de Valor Presente Neto (NPV)")
|
| 15 |
+
|
| 16 |
+
# 3. ENTRADAS PRINCIPALES
|
| 17 |
+
# 3.1 Tasa de descuento con slider (0 % – 50 %)
|
| 18 |
+
discount_rate_pct = st.slider(
|
| 19 |
+
"Selecciona la tasa de descuento (%)",
|
| 20 |
+
min_value=0.0,
|
| 21 |
+
max_value=50.0,
|
| 22 |
+
value=10.0,
|
| 23 |
+
step=0.1,
|
| 24 |
+
help="Costo de oportunidad o rendimiento mínimo aceptable."
|
| 25 |
+
)
|
| 26 |
+
discount_rate = discount_rate_pct / 100 # Convertir a proporción
|
| 27 |
+
|
| 28 |
+
# 3.2 Duración del proyecto en años
|
| 29 |
+
years = st.slider(
|
| 30 |
+
"¿Cuántos años dura el proyecto?",
|
| 31 |
+
min_value=1,
|
| 32 |
+
max_value=30,
|
| 33 |
+
value=5,
|
| 34 |
+
step=1
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
st.markdown("---")
|
| 38 |
+
|
| 39 |
+
# 4. CAPTURA DE FLUJOS DE CAJA MEDIANTE TIMELINE
|
| 40 |
+
st.subheader("Ingresa los flujos de caja anuales")
|
| 41 |
+
|
| 42 |
+
with st.form("cashflow_form", clear_on_submit=False):
|
| 43 |
+
# Usamos columnas para generar un "timeline" con recuadros
|
| 44 |
+
cols = st.columns(years + 1) # +1 porque suele haber flujo en t=0
|
| 45 |
+
cashflows = []
|
| 46 |
+
for t in range(years + 1):
|
| 47 |
+
with cols[t]:
|
| 48 |
+
cf = st.number_input(
|
| 49 |
+
f"Año {t}",
|
| 50 |
+
value=0.0,
|
| 51 |
+
key=f"cf_{t}",
|
| 52 |
+
format="%.2f"
|
| 53 |
+
)
|
| 54 |
+
cashflows.append(cf)
|
| 55 |
+
|
| 56 |
+
submitted = st.form_submit_button("Calcular NPV")
|
| 57 |
+
|
| 58 |
+
# 5. CÁLCULO DEL NPV
|
| 59 |
+
if submitted:
|
| 60 |
+
npv = sum(cf / (1 + discount_rate) ** t for t, cf in enumerate(cashflows))
|
| 61 |
+
|
| 62 |
+
st.markdown("## Resultado")
|
| 63 |
+
st.metric(label="Valor Presente Neto (NPV)", value=f"${npv:,.2f}")
|
| 64 |
+
|
| 65 |
+
# 6. VISUALIZACIÓN DEL TIMELINE CON ESTILO
|
| 66 |
+
st.markdown("### Timeline de Flujos de Caja")
|
| 67 |
+
timeline_cols = st.columns(years + 1)
|
| 68 |
+
for t, col in enumerate(timeline_cols):
|
| 69 |
+
with col:
|
| 70 |
+
st.markdown(
|
| 71 |
+
f"""
|
| 72 |
+
<div style='border:1px solid #999; border-radius:6px;
|
| 73 |
+
padding:10px; text-align:center;'>
|
| 74 |
+
<strong>Año {t}</strong><br>
|
| 75 |
+
${cashflows[t]:,.2f}
|
| 76 |
+
</div>
|
| 77 |
+
""",
|
| 78 |
+
unsafe_allow_html=True
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# 7. INTERPRETACIÓN BÁSICA
|
| 82 |
+
if npv > 0:
|
| 83 |
+
st.success("El proyecto genera valor (NPV positivo).")
|
| 84 |
+
elif npv < 0:
|
| 85 |
+
st.error("El proyecto destruye valor (NPV negativo).")
|
| 86 |
+
else:
|
| 87 |
+
st.info("El proyecto es neutro (NPV = 0).")
|