Trireme commited on
Commit
dd5d862
·
1 Parent(s): 43fd4b6

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +63 -6
app.py CHANGED
@@ -223,6 +223,10 @@ async def process_data():
223
 
224
  return(starting_df,final_df.tail(1),jup_df.tail(1),df_with_totals.tail(1))
225
 
 
 
 
 
226
  async def display_data():
227
  df0, df1, df2, df3 = await process_data()
228
 
@@ -232,11 +236,47 @@ async def display_data():
232
  return df, total_value
233
 
234
  df0, total0 = summarize(df0)
 
 
235
  df3, total3 = summarize(df3)
236
 
237
  pnl = float(total3) - float(total0)
238
 
239
- return summarize(df0) + summarize(df1) + summarize(df2) + summarize(df3) + (f"PNL : {pnl:,.2f}"," ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
 
241
  with gr.Blocks() as demo:
242
  gr.Markdown("## BILLY Balances and PnL")
@@ -256,13 +296,30 @@ with gr.Blocks() as demo:
256
 
257
  pnl_out = gr.Textbox(label="PNL (AUM Model)")
258
 
259
- # Set up the function to run on app load
260
  demo.load(
261
- fn=display_data,
262
  inputs=[],
263
- outputs=[df0_out, txt0_out, df1_out, txt1_out, df2_out, txt2_out, df3_out, txt3_out, pnl_out]
 
 
 
 
 
 
264
  )
265
 
266
- btn.click(display_data, outputs=[df0_out, txt0_out, df1_out, txt1_out, df2_out, txt2_out, df3_out,txt3_out,pnl_out])
 
 
 
 
 
 
 
 
 
 
 
267
 
268
- demo.launch(debug=True,share=True)
 
223
 
224
  return(starting_df,final_df.tail(1),jup_df.tail(1),df_with_totals.tail(1))
225
 
226
+ client = MongoClient('mongodb+srv://djamaal:FHmV2N733lzrLkWf@test-cluster.mys4n.mongodb.net/')
227
+ db = client["billy_balances"]
228
+ collection = db["balance_data"]
229
+
230
  async def display_data():
231
  df0, df1, df2, df3 = await process_data()
232
 
 
236
  return df, total_value
237
 
238
  df0, total0 = summarize(df0)
239
+ df1, total1 = summarize(df1)
240
+ df2, total2 = summarize(df2)
241
  df3, total3 = summarize(df3)
242
 
243
  pnl = float(total3) - float(total0)
244
 
245
+ # Save to MongoDB
246
+ record = {
247
+ "timestamp": datetime.datetime.utcnow(),
248
+ "df0": df0.to_dict(),
249
+ "total0": total0,
250
+ "df1": df1.to_dict(),
251
+ "total1": total1,
252
+ "df2": df2.to_dict(),
253
+ "total2": total2,
254
+ "df3": df3.to_dict(),
255
+ "total3": total3,
256
+ "pnl": pnl
257
+ }
258
+ collection.insert_one(record)
259
+
260
+ return df0, total0, df1, total1, df2, total2, df3, total3, f"PNL : {pnl:,.2f}"
261
+
262
+ def load_from_mongo():
263
+ latest = collection.find_one(sort=[("timestamp", -1)])
264
+ if latest:
265
+ df0 = pd.DataFrame(latest["df0"])
266
+ df1 = pd.DataFrame(latest["df1"])
267
+ df2 = pd.DataFrame(latest["df2"])
268
+ df3 = pd.DataFrame(latest["df3"])
269
+
270
+ return (
271
+ df0, latest["total0"],
272
+ df1, latest["total1"],
273
+ df2, latest["total2"],
274
+ df3, latest["total3"],
275
+ f"PNL : {latest['pnl']:,.2f}"
276
+ )
277
+ else:
278
+ empty_df = pd.DataFrame()
279
+ return empty_df, 0, empty_df, 0, empty_df, 0, empty_df, 0, "PNL : 0.00"
280
 
281
  with gr.Blocks() as demo:
282
  gr.Markdown("## BILLY Balances and PnL")
 
296
 
297
  pnl_out = gr.Textbox(label="PNL (AUM Model)")
298
 
299
+ # Load from MongoDB on app load (sync function)
300
  demo.load(
301
+ fn=load_from_mongo,
302
  inputs=[],
303
+ outputs=[
304
+ df0_out, txt0_out,
305
+ df1_out, txt1_out,
306
+ df2_out, txt2_out,
307
+ df3_out, txt3_out,
308
+ pnl_out
309
+ ]
310
  )
311
 
312
+ # Async button click compute and store new data
313
+ btn.click(
314
+ fn=display_data,
315
+ inputs=[],
316
+ outputs=[
317
+ df0_out, txt0_out,
318
+ df1_out, txt1_out,
319
+ df2_out, txt2_out,
320
+ df3_out, txt3_out,
321
+ pnl_out
322
+ ]
323
+ )
324
 
325
+ demo.launch(debug=True, share=True)