Kfong commited on
Commit
e35c449
1 Parent(s): f1af214

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -4,33 +4,33 @@ import numpy as np
4
  import plotly.express as px
5
 
6
  def calculate_present_value(cash_flows, discount_rate):
7
- years = np.arange(1, len(cash_flows)+1)
8
  present_values = cash_flows / (1 + discount_rate) ** years
9
  return np.sum(present_values)
10
 
11
- st.title('Calcula el Valor Presente')
12
 
13
- # Input del usuario para cash flows
14
- st.subheader('Cash Flows x año:')
15
- cash_flows = []
16
- for i in range(1, 11):
17
- cash_flow = st.number_input(f"Cash Flow {i}:", value=10000.0, step=1000.0, format="%.2f")
18
- cash_flows.append(cash_flow)
 
 
 
 
19
 
20
  # Input del usuario para la tasa de descuento
21
- discount_rate = st.number_input('Tasa de descuento (ejemplo: 0.05 para 5%):', value=0.05, step=0.01, format="%.2f")
22
 
 
23
  if st.button('Calcular Valor Presente'):
24
- cash_flows = np.array(cash_flows)
 
25
  pv = calculate_present_value(cash_flows, discount_rate)
26
  st.success(f"El valor presente calculado es: ${pv:,.2f}")
27
 
28
- # Crear DataFrame para visualización
29
- df = pd.DataFrame({
30
- 'Año': np.arange(1, 11),
31
- 'Cash Flow': cash_flows
32
- })
33
-
34
  # Gráfico
35
- fig = px.scatter(df, x='Año', y='Cash Flow', title="Cash Flows vs. Tiempo", labels={'Cash Flow': 'Cash Flow ($)', 'Año': 'Año'})
36
  st.plotly_chart(fig)
 
4
  import plotly.express as px
5
 
6
  def calculate_present_value(cash_flows, discount_rate):
7
+ years = np.arange(1, len(cash_flows) + 1)
8
  present_values = cash_flows / (1 + discount_rate) ** years
9
  return np.sum(present_values)
10
 
11
+ st.title('Calculadora de Valor Presente')
12
 
13
+ # Inicializar DataFrame para cash flows
14
+ if 'cash_flows' not in st.session_state:
15
+ st.session_state.cash_flows = pd.DataFrame({
16
+ 'Año': range(1, 11),
17
+ 'Cash Flow': [10000.0] * 10 # Inicializar con un valor default
18
+ })
19
+
20
+ # Mostrar la tabla editable
21
+ st.subheader('Ingrese los Cash Flows anuales:')
22
+ cash_flow_table = st.dataframe(st.session_state.cash_flows)
23
 
24
  # Input del usuario para la tasa de descuento
25
+ discount_rate = st.number_input('Ingrese la tasa de descuento (ejemplo: 0.05 para 5%):', value=0.05, step=0.01, format="%.2f")
26
 
27
+ # Botón para calcular el valor presente
28
  if st.button('Calcular Valor Presente'):
29
+ # Extraer los cash flows desde la tabla
30
+ cash_flows = st.session_state.cash_flows['Cash Flow'].values
31
  pv = calculate_present_value(cash_flows, discount_rate)
32
  st.success(f"El valor presente calculado es: ${pv:,.2f}")
33
 
 
 
 
 
 
 
34
  # Gráfico
35
+ fig = px.scatter(st.session_state.cash_flows, x='Año', y='Cash Flow', title="Cash Flows vs. Tiempo", labels={'Cash Flow': 'Cash Flow ($)', 'Año': 'Año'})
36
  st.plotly_chart(fig)