# -*- coding: utf-8 -*- 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["HYPER_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 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}" ) 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" if __name__ == "__main__": with gr.Blocks() as demo: gr.Markdown("## HYPER 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") reward_out = gr.Textbox(label="LP Rewards") 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 ] ) demo.launch(debug=True, share=True)