lavalley12 commited on
Commit
ebd572b
1 Parent(s): 3b12323

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -32
app.py CHANGED
@@ -1,39 +1,40 @@
1
- from flask import Flask, render_template, request, jsonify
2
  import numpy as np
 
3
  import matplotlib.pyplot as plt
4
 
5
- app = Flask(__name__)
 
 
 
6
 
7
- def calcular_vpn(n, r, cash_flow):
8
- descuento = 1 / ((1 + r) ** np.arange(1, n + 1))
9
- vpn = np.sum(cash_flow * descuento)
10
- return vpn
11
 
12
- @app.route('/')
13
- def index():
14
- return render_template('index.html')
15
 
16
- @app.route('/calcular', methods=['POST'])
17
- def calcular():
18
- data = request.get_json()
19
- n = int(data['n'])
20
- r = float(data['r'])
21
- cash_flow = np.array(data['cash_flow'])
22
-
23
- vpn = calcular_vpn(n, r, cash_flow)
24
-
25
- plt.figure(figsize=(10, 6))
26
- plt.plot(np.arange(1, n + 1), cash_flow, marker='o')
27
- plt.xlabel('Años')
28
- plt.ylabel('Cash Flow')
29
- plt.title('Flujo de Efectivo')
30
- plt.grid(True)
31
-
32
- # Guardar el gráfico como imagen
33
- img_path = 'static/plot.png'
34
- plt.savefig(img_path)
35
-
36
- return jsonify({'vpn': vpn, 'plot_path': img_path})
37
 
38
- if __name__ == '__main__':
39
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
  import numpy as np
3
+ import pandas as pd
4
  import matplotlib.pyplot as plt
5
 
6
+ # Function to compute present value
7
+ def calculate_pv(cashflows, rate):
8
+ pv = sum(cf / (1 + rate) ** i for i, cf in enumerate(cashflows))
9
+ return pv
10
 
11
+ # Streamlit app layout
12
+ st.title('Cashflow Present Value Calculator')
 
 
13
 
14
+ # Input for the discount rate
15
+ rate = st.number_input('Discount Rate (%)', min_value=0.0, max_value=100.0, value=15.0, step=0.1)/100
 
16
 
17
+ # Inputs for the number of years
18
+ n = st.number_input("How many years?", min_value = 0, step=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # Inputs for the cashflows
21
+ cf_list = [0] * (n+1)
22
+ cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(int)
23
+ cf_df = st.data_editor(cf_dict.T)
24
+
25
+ cashflows = np.array(cf_df.values[0])
26
+
27
+ # Button to calculate PV
28
+ if st.button('Calculate Present Value'):
29
+ pv = calculate_pv(cashflows, rate)
30
+ st.write(f'The Present Value is: ${pv:,.2f}')
31
+
32
+ # Prepare data for the plot
33
+ colors = ['green' if cf > 0 else 'red' for cf in cashflows]
34
+ plt.scatter(range(n+1), cashflows, c=colors)
35
+ plt.title('Cashflow Diagram')
36
+ plt.xlabel('Period')
37
+ plt.ylabel('Cashflow')
38
+ for i, cf in enumerate(cashflows):
39
+ plt.annotate(f'{cf:,.2f}', (i, cf), textcoords="offset points", xytext=(0,n), ha='center')
40
+ st.pyplot(plt)