EricSam commited on
Commit
a7c5e5f
Β·
verified Β·
1 Parent(s): 598d35e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -139,37 +139,37 @@ def update_performance_chart(trades):
139
  month = datetime.fromtimestamp(trade['time'] / 1000).strftime('%b')
140
  monthly_pl[month] = monthly_pl.get(month, 0) + trade['income']
141
  chart_data = {
142
- 'labels': list(monthly_pl.keys()),
143
- 'datasets': [{'label': 'Profit/Loss', 'data': list(monthly_pl.values()), 'borderColor': '#1E90FF', 'backgroundColor': 'rgba(30, 144, 255, 0.1)', 'tension': 0.4, 'fill': True}]
 
 
 
 
144
  }
145
- return gr.Chart(value=chart_data, type="line", options={
146
- 'responsive': True,
147
- 'plugins': {'legend': {'display': False}},
148
- 'scales': {'y': {'grid': {'color': 'rgba(0, 0, 0, 0.05)', 'borderDash': [5]}, 'ticks': {'callback': lambda value: f'${value}'}}, 'x': {'grid': {'display': False}}}
149
- })
150
 
151
  def update_allocation_chart(allocation):
152
  chart_data = {
153
- 'labels': allocation['labels'],
154
- 'datasets': [{'data': allocation['data'], 'backgroundColor': ['#1E90FF', '#10b981', '#8b5cf6', '#f59e0b'], 'borderWidth': 0}]
 
 
155
  }
156
  legend_html = "\n".join(f"""
157
  <div class="mb-3">
158
  <div class="flex justify-between mb-1">
159
  <span class="text-gray-500 dark:text-gray-400 flex items-center">
160
- <span class="h-3 w-3 bg-[{chart_data['datasets'][0]['backgroundColor'][i]}] rounded-full mr-2"></span>
161
  {label}
162
  </span>
163
  <span class="font-medium dark:text-white">{data:.1f}%</span>
164
  </div>
165
  <div class="w-full bg-gray-200 rounded-full h-2 dark:bg-gray-700">
166
- <div class="bg-[{chart_data['datasets'][0]['backgroundColor'][i]}] h-2 rounded-full" style="width: {data}%"></div>
167
  </div>
168
  </div>
169
  """ for i, (label, data) in enumerate(zip(allocation['labels'], allocation['data'])))
170
- return gr.Chart(value=chart_data, type="doughnut", options={
171
- 'responsive': True, 'cutout': '65%', 'plugins': {'legend': {'display': False}, 'tooltip': {'callbacks': {'label': lambda context: f"{context['label']}: {context['parsed']}%"}}}
172
- }), gr.HTML(legend_html)
173
 
174
  # Main Data Fetch and Update Function
175
  def update_dashboard(api_key, api_secret):
@@ -198,7 +198,7 @@ def update_dashboard(api_key, api_secret):
198
  except Exception as e:
199
  return (
200
  "$Loading...", 0, "0 Long β€’ 0 Short", "$0.00", "Low", "0%", gr.HTML("<tr><td colspan='8' class='py-4 text-center'>Error: Failed to sync with BingX API - Check credentials</td></tr>"),
201
- gr.HTML("<div>Error loading stats</div>"), gr.Chart(), (gr.Chart(), gr.HTML("")), "Just now", "Just now"
202
  )
203
 
204
  # Save Credentials Function
@@ -267,7 +267,7 @@ with gr.Blocks(title="Nakhoda4X Pro") as demo:
267
  # Charts and Statistics
268
  with gr.Row(elem_classes="grid grid-cols-1 lg:grid-cols-3 gap-6"):
269
  with gr.Column(scale=2):
270
- performance_chart = gr.Chart()
271
  gr.HTML("<div class='flex justify-between items-center mb-6'><h3 class='text-lg font-bold text-gray-800 dark:text-white'>Monthly Performance</h3><div class='flex space-x-3'><button class='px-3 py-1 rounded-lg text-sm bg-gray-100 dark:bg-gray-800 text-gray-800 dark:text-white'>1M</button><button class='px-3 py-1 rounded-lg text-sm bg-primary text-white'>3M</button><button class='px-3 py-1 rounded-lg text-sm bg-gray-100 dark:bg-gray-800 text-gray-800 dark:text-white'>6M</button><button class='px-3 py-1 rounded-lg text-sm bg-gray-100 dark:bg-gray-800 text-gray-800 dark:text-white'>1Y</button></div></div>")
272
  with gr.Column(scale=1):
273
  advanced_stats = gr.HTML()
@@ -294,7 +294,7 @@ with gr.Blocks(title="Nakhoda4X Pro") as demo:
294
  portfolio_allocation = gr.Blocks()
295
  with portfolio_allocation:
296
  gr.HTML("<div class='bg-white dark:bg-darkCard rounded-2xl shadow-md p-6'><div class='flex justify-between items-center mb-6'><h3 class='text-lg font-bold text-gray-800 dark:text-white'>Portfolio Allocation</h3><div><span id='allocation-update' class='text-gray-500 dark:text-gray-400 text-sm'>Last updated: Just now</span></div></div>")
297
- allocation_chart, allocation_legend = gr.Chart(), gr.HTML()
298
 
299
  # Event Handlers
300
  toggle_btn.click(fn=toggle_api_form, inputs=[show_form], outputs=[api_form], _js="() => !document.querySelector('.api-form').classList.contains('open')")
 
139
  month = datetime.fromtimestamp(trade['time'] / 1000).strftime('%b')
140
  monthly_pl[month] = monthly_pl.get(month, 0) + trade['income']
141
  chart_data = {
142
+ 'x': list(monthly_pl.keys()),
143
+ 'y': list(monthly_pl.values()),
144
+ 'color': ['#1E90FF'] * len(monthly_pl),
145
+ 'title': 'Monthly Performance',
146
+ 'x_title': 'Month',
147
+ 'y_title': 'Profit/Loss ($)'
148
  }
149
+ return gr.LinePlot(value=chart_data, x='x', y='y', color='color', title='Monthly Performance', x_title='Month', y_title='Profit/Loss ($)')
 
 
 
 
150
 
151
  def update_allocation_chart(allocation):
152
  chart_data = {
153
+ 'names': allocation['labels'],
154
+ 'values': allocation['data'],
155
+ 'colors': ['#1E90FF', '#10b981', '#8b5cf6', '#f59e0b'],
156
+ 'title': 'Portfolio Allocation'
157
  }
158
  legend_html = "\n".join(f"""
159
  <div class="mb-3">
160
  <div class="flex justify-between mb-1">
161
  <span class="text-gray-500 dark:text-gray-400 flex items-center">
162
+ <span class="h-3 w-3 bg-[{chart_data['colors'][i % 4]}] rounded-full mr-2"></span>
163
  {label}
164
  </span>
165
  <span class="font-medium dark:text-white">{data:.1f}%</span>
166
  </div>
167
  <div class="w-full bg-gray-200 rounded-full h-2 dark:bg-gray-700">
168
+ <div class="bg-[{chart_data['colors'][i % 4]}] h-2 rounded-full" style="width: {data}%"></div>
169
  </div>
170
  </div>
171
  """ for i, (label, data) in enumerate(zip(allocation['labels'], allocation['data'])))
172
+ return gr.PiePlot(value=chart_data, name='names', value='values', color='colors', title='Portfolio Allocation'), gr.HTML(legend_html)
 
 
173
 
174
  # Main Data Fetch and Update Function
175
  def update_dashboard(api_key, api_secret):
 
198
  except Exception as e:
199
  return (
200
  "$Loading...", 0, "0 Long β€’ 0 Short", "$0.00", "Low", "0%", gr.HTML("<tr><td colspan='8' class='py-4 text-center'>Error: Failed to sync with BingX API - Check credentials</td></tr>"),
201
+ gr.HTML("<div>Error loading stats</div>"), gr.LinePlot(), (gr.PiePlot(), gr.HTML("")), "Just now", "Just now"
202
  )
203
 
204
  # Save Credentials Function
 
267
  # Charts and Statistics
268
  with gr.Row(elem_classes="grid grid-cols-1 lg:grid-cols-3 gap-6"):
269
  with gr.Column(scale=2):
270
+ performance_chart = gr.LinePlot()
271
  gr.HTML("<div class='flex justify-between items-center mb-6'><h3 class='text-lg font-bold text-gray-800 dark:text-white'>Monthly Performance</h3><div class='flex space-x-3'><button class='px-3 py-1 rounded-lg text-sm bg-gray-100 dark:bg-gray-800 text-gray-800 dark:text-white'>1M</button><button class='px-3 py-1 rounded-lg text-sm bg-primary text-white'>3M</button><button class='px-3 py-1 rounded-lg text-sm bg-gray-100 dark:bg-gray-800 text-gray-800 dark:text-white'>6M</button><button class='px-3 py-1 rounded-lg text-sm bg-gray-100 dark:bg-gray-800 text-gray-800 dark:text-white'>1Y</button></div></div>")
272
  with gr.Column(scale=1):
273
  advanced_stats = gr.HTML()
 
294
  portfolio_allocation = gr.Blocks()
295
  with portfolio_allocation:
296
  gr.HTML("<div class='bg-white dark:bg-darkCard rounded-2xl shadow-md p-6'><div class='flex justify-between items-center mb-6'><h3 class='text-lg font-bold text-gray-800 dark:text-white'>Portfolio Allocation</h3><div><span id='allocation-update' class='text-gray-500 dark:text-gray-400 text-sm'>Last updated: Just now</span></div></div>")
297
+ allocation_chart, allocation_legend = gr.PiePlot(), gr.HTML()
298
 
299
  # Event Handlers
300
  toggle_btn.click(fn=toggle_api_form, inputs=[show_form], outputs=[api_form], _js="() => !document.querySelector('.api-form').classList.contains('open')")