Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
# Function to compute present value | |
def calculate_pv(cashflows, rate): | |
pv = sum(cf / (1 + rate) ** i for i, cf in enumerate(cashflows)) | |
return pv | |
# Streamlit app layout | |
st.title('Cashflow Present Value Calculator') | |
# Input for the discount rate | |
rate = st.slider('Discount Rate (%)', min_value=0.0, max_value=100.0, value=15.0, step=0.1)/100 | |
# Inputs for the number of years | |
n = st.number_input("How many years?", min_value = 0, step=1) | |
# Inputs for the cashflows | |
cf_list = [0] * (n+1) # Asegurémonos de que hay un elemento por cada período | |
cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(int) | |
cf_df = st.text_area("Enter Cashflows (comma-separated)", value=', '.join(map(str, cf_list))) | |
cashflows = np.array([float(cf.strip()) for cf in cf_df.split(',')]) | |
# Añadimos un elemento si el número de elementos no coincide con n + 1 | |
if len(cashflows) != n + 1: | |
st.error("Please enter the correct number of cashflows.") | |
st.stop() | |
# Button to calculate PV | |
if st.button('Calculate Present Value'): | |
pv = calculate_pv(cashflows, rate) | |
st.write(f'The Present Value is: ${pv:,.2f}') | |
# Plotting cashflow diagram | |
fig, ax = plt.subplots(figsize=(8, 6)) | |
colors = ['green' if cf > 0 else 'red' for cf in cashflows] | |
ax.plot(range(n+1), cashflows, marker='o', color='b', linestyle='-') | |
ax.fill_between(range(n+1), cashflows, color=colors, alpha=0.3) | |
ax.set_title('Cashflow Diagram') | |
ax.set_xlabel('Period') | |
ax.set_ylabel('Cashflow') | |
for i, cf in enumerate(cashflows): | |
ax.text(i, cf, f'{cf:,.2f}', ha='center', va='bottom') | |
ax.grid(True) | |
st.pyplot(fig) |