Skip to content

Commit

Permalink
#76 is down to 2 TODOs, one solved
Browse files Browse the repository at this point in the history
  • Loading branch information
epogrebnyak committed Jan 14, 2024
1 parent 835eaf8 commit 1d20548
Showing 1 changed file with 49 additions and 46 deletions.
95 changes: 49 additions & 46 deletions streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
# Как запустить:
# How to run:
#
# git clone https://github.com/epogrebnyak/abacus.git
# pip install -e .
# streamlit run streamlit_app.py
#
# Что сделать: TODO-1, TODO-2, TODO-3.
# Что сделать: TODO-1, TODO-2.
#
import streamlit as st

if "entries" not in st.session_state:
st.session_state["entries"] = []


def add_entry(dr, cr, a):
st.session_state["entries"] += [Entry(dr, cr, a)]


def to_int(a):
try:
return int(a)
except ValueError:
return 0


def last_posted_caption():
es = st.session_state["entries"]
if not es:
st.caption("Nothing posted yet.")
else:
e = es[-1]
st.caption(f"Last posted: {e.debit}, {e.credit}, {e.amount}")


labels = ["cash", "ar", "inventory", "equity", "sales", "cogs", "sga"]
with st.sidebar:
st.header("Post double entry")
with st.form("add_entry"):
dr = st.selectbox("Debit:", labels, index=0)
cr = st.selectbox("Credit:", labels, index=3)
a = to_int(st.text_input("Amount (only integer)"))
st.form_submit_button(label="Post entry", on_click=add_entry, args=(dr, cr, a))
# TODO-2: My scenario:
# - I run the app
# - I add amount 500, then click Post entry button
# expected to see is (cash, equity, 500), but I get
# (cash, equity, 500) - which I never submitted.
# - I add amount 200, then click Post entry button,
# - after this I see (cash, equity, 500), so
# last_posted_caption() lags to show value by one click.
# Very strange!
last_posted_caption()


st.header("Accounting with `abacus` :star:")

from abacus import Chart, Report
Expand All @@ -34,65 +78,24 @@
),
)

if "entries" not in st.session_state:
st.session_state["entries"] = []


def live_balance_sheet():
ledger = chart.ledger().post_many(st.session_state["entries"])
report = Report(chart, ledger)
return report.balance_sheet


# TODO-1: текст ниже должен обновляться в момент добавления новых записей через форму
st.text(live_balance_sheet())

# TODO-2: текст ниже должен обновляться в момент добавления новых записей через форму
# сейчас добавляется с задержкой в одну итерацию
# видимо первая запись - st.session_state["entries"] это дефолтный значения формы с 0,
# хотя она не нажимаются.
st.text(live_balance_sheet())

"Введенные через форму проводки (показывает или добавляет с задержкой на одну итерацию):"
st.write(st.session_state["entries"])


def add_entry(dr, cr, a):
st.session_state["entries"] += [Entry(dr, cr, a)]


def form_callback(dr, cr, a):
add_entry(dr, cr, a)
update_done_posting(dr, cr, a)


def to_int(a):
try:
return int(a)
except ValueError:
return 0


def update_done_posting(dr, cr, a):
done_posting.text(f"Posted entry: {dr}, {cr}, {a}")


# st.write(list(chart.to_dict().keys()))

labels = ["cash", "ar", "inventory", "equity", "sales", "cogs", "sga"]
with st.sidebar:
st.header("Post double entry")
with st.form("add_entry"):
dr = st.selectbox("Debit:", labels, index=0)
cr = st.selectbox("Credit:", labels, index=3)
a = to_int(st.text_input("Amount (only integer)"))
st.form_submit_button(
label="Post entry", on_click=form_callback, args=(dr, cr, a)
)
# TODO-2: нужно обновлять подпись ниже после нажатия кнопки формы
# (не работает сейчас)
# TODO-3: эта подпись желательно в формате st.caption "Last posted:""
done_posting = st.empty()
st.caption("Last posted:")


# with st.form("chart_form"):
# st.write("Define chart of accounts. Separate account names by space.")
# assets = st.text_input("Assets", value="cash ar goods")
Expand Down

0 comments on commit 1d20548

Please sign in to comment.