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.number_input('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) | |
cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(int) | |
cf_df = st.data_editor(cf_dict.T) | |
cashflows = np.array(cf_df.values[0]) | |
# Button to calculate PV | |
if st.button('Calculate Present Value'): | |
pv = calculate_pv(cashflows, rate) | |
st.write(f'The Present Value is: ${pv:,.2f}') | |
# Prepare data for the plot | |
colors = ['green' if cf > 0 else 'red' for cf in cashflows] | |
plt.scatter(range(n+1), cashflows, c=colors) | |
plt.title('Cashflow Diagram') | |
plt.xlabel('Period') | |
plt.ylabel('Cashflow') | |
for i, cf in enumerate(cashflows): | |
plt.annotate(f'{cf:,.2f}', (i, cf), textcoords="offset points", xytext=(0,n), ha='center') | |
st.pyplot(plt) | |