from pymongo import MongoClient import pandas as pd import requests import json import urllib.parse import gradio as gr import datetime import os client = MongoClient(os.getenv('MONGO_DB_URI')) db = client["Patchy_balances"] collection = db["balance_data"] def load_from_mongo(): latest = collection.find_one(sort=[("timestamp", -1)]) if latest: df0 = pd.DataFrame(latest["df0"]).T df1 = pd.DataFrame(latest["df1"]).T df2 = pd.DataFrame(latest["df2"]).T df3 = pd.DataFrame(latest["df3"]).T df4 = pd.DataFrame(latest["df4"]).T df5 = pd.DataFrame(latest["df5"]).T return ( df0, latest["total0"], df1, latest["total1"], df2, latest["total2"], df3, latest["total3"], df4, latest["total4"], f"PNL : {latest['pnl']:,.2f}", f"LP Rewards : {latest['rewards']:,.2f}", df5 ) else: empty_df = pd.DataFrame() return empty_df, 0, empty_df, 0, empty_df, 0, empty_df, 0, empty_df, 0,"PNL : $0.00","LP Rewards : $0.00",empty_df if __name__ == "__main__": with gr.Blocks() as demo: gr.Markdown("## Patchy Balances and PnL") df0_out = gr.Dataframe(label="Starting Balances") txt0_out = gr.Textbox(label="Starting Value Total ($)") df2_out = gr.Dataframe(label="Wallet Balances") txt2_out = gr.Textbox(label="Wallet Value Total ($)") df3_out = gr.Dataframe(label="JUP Limit Balances") txt3_out = gr.Textbox(label="JUP Limit Value Total ($)") df4_out = gr.Dataframe(label="LP Balances") txt4_out = gr.Textbox(label="LP Balances Value Total ($)") df5_out = gr.Dataframe(label="LP Reward Balances") reward_out = gr.Textbox(label="LP Reward Value Total ($)") df1_out = gr.Dataframe(label="Total Balances") txt1_out = gr.Textbox(label="Current Value Total ($)") pnl_out = gr.Textbox(label="PNL (AUM Model)") # Load from MongoDB on app load (sync function) demo.load( fn=load_from_mongo, inputs=[], outputs=[ df0_out, txt0_out, df1_out, txt1_out, df2_out, txt2_out, df3_out, txt3_out, df4_out, txt4_out, pnl_out,reward_out, df5_out ] ) demo.launch(debug=True, share=True)