a-ge commited on
Commit
9aaa708
·
verified ·
1 Parent(s): 4816d61

Update app.py

Browse files

refactoring gradio components

Files changed (1) hide show
  1. app.py +26 -38
app.py CHANGED
@@ -47,7 +47,24 @@ def get_technical_analysis(
47
  return {"Error": str(e)}
48
 
49
 
50
- def get_comparison_details_and_generate_report(symbol: str, benchmark: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  """Get the symbol performance against provided benchmark and return plots and HTML report content.
52
 
53
  Args:
@@ -57,18 +74,6 @@ def get_comparison_details_and_generate_report(symbol: str, benchmark: str):
57
 
58
  data = qs.utils.download_returns(symbol)
59
 
60
- # Snapshot plot to in-memory image
61
- snapshot_buf = BytesIO()
62
- qs.plots.snapshot(data, title="Performance", savefig=snapshot_buf)
63
- snapshot_buf.seek(0)
64
- snapshot_img = Image.open(snapshot_buf)
65
-
66
- # Yearly returns plot to in-memory image
67
- returns_buf = BytesIO()
68
- qs.plots.yearly_returns(data, benchmark=benchmark, savefig=returns_buf)
69
- returns_buf.seek(0)
70
- returns_img = Image.open(returns_buf)
71
-
72
  # Generate and read HTML report
73
  report_path = "performance_report.html"
74
  qs.reports.html(
@@ -81,20 +86,14 @@ def get_comparison_details_and_generate_report(symbol: str, benchmark: str):
81
  with open(report_path, "r", encoding="utf-8") as file:
82
  report_content = file.read()
83
 
84
- return snapshot_img, returns_img, report_content, report_path
85
-
86
-
87
- def gradio_interface(symbol: str, benchmark: str):
88
- """Gradio interface function to generate and display the report and plots."""
89
- snapshot_img, returns_img, report_content, report_path = (
90
- get_comparison_details_and_generate_report(symbol, benchmark)
91
- )
92
- return snapshot_img, report_content, report_path, returns_img
93
 
94
 
95
  with gr.Blocks() as demo:
96
- gr.Markdown("# Trade-lens🔎")
97
- gr.Markdown("Get Analyst ratings and technical indicator details")
 
 
98
 
99
  with gr.Tab("Technical Analysis"):
100
  symbol_input = gr.Textbox(
@@ -117,7 +116,7 @@ with gr.Blocks() as demo:
117
  )
118
 
119
  with gr.Tab("Performance Comparison"):
120
- with gr.Blocks() as interface:
121
  gr.Markdown("# Stock Performance Analyzer")
122
  gr.Markdown(
123
  "Enter a stock symbol and a benchmark to generate a performance report and snapshot."
@@ -131,35 +130,24 @@ with gr.Blocks() as demo:
131
  info="Some symbols may require a dot(.)suffix of corresponding exchange as TCS.NS",
132
  )
133
  benchmark_input = gr.Textbox(
134
- label="Benchmark Symbol (e.g., ^DJI,^NSEI,^UKX,SPY)",
135
  placeholder="Enter benchmark symbol",
136
  info="For index use (^) as that is the accepted format. It can also be other valid stocks/symbols too.",
137
  )
138
 
139
  generate_button = gr.Button("Generate Report", variant="primary")
140
- returns_output = gr.Image(label="Yearly Returns")
141
 
142
- with gr.Column():
143
  download_button = gr.File(label="Download Report")
144
- snapshot_output = gr.Image(label="Performance Snapshot")
145
 
146
  with gr.Row():
147
  report_output = gr.HTML(label="Performance Report")
148
 
149
- def generate_report(symbol, benchmark):
150
- snapshot_img, report_html, report_path, returns_img = gradio_interface(
151
- symbol, benchmark
152
- )
153
- return snapshot_img, report_html, report_path, returns_img
154
-
155
  generate_button.click(
156
- generate_report,
157
  inputs=[symbol_input, benchmark_input],
158
  outputs=[
159
- snapshot_output,
160
  report_output,
161
  download_button,
162
- returns_output,
163
  ],
164
  )
165
 
 
47
  return {"Error": str(e)}
48
 
49
 
50
+ def get_performance_snapshot(symbol) -> Image:
51
+ """Get the symbol performance snapshot and returns plot image.
52
+
53
+ Args:
54
+ data (Series:[float])
55
+
56
+ Returns:
57
+ Image of the performance snapshot is returned
58
+ """
59
+ _data = qs.utils.download_returns(symbol)
60
+
61
+ snapshot_buf = BytesIO()
62
+ qs.plots.snapshot(_data, title="Performance", savefig=snapshot_buf)
63
+ snapshot_buf.seek(0)
64
+ return Image.open(snapshot_buf)
65
+
66
+
67
+ def get_comparison_report(symbol: str, benchmark: str):
68
  """Get the symbol performance against provided benchmark and return plots and HTML report content.
69
 
70
  Args:
 
74
 
75
  data = qs.utils.download_returns(symbol)
76
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # Generate and read HTML report
78
  report_path = "performance_report.html"
79
  qs.reports.html(
 
86
  with open(report_path, "r", encoding="utf-8") as file:
87
  report_content = file.read()
88
 
89
+ return report_content, report_path
 
 
 
 
 
 
 
 
90
 
91
 
92
  with gr.Blocks() as demo:
93
+ gr.Markdown("# Stock-lens🔎")
94
+ gr.Markdown(
95
+ "Get Analyst ratings and technical indicator details📈. Get Comparison of your favourite stocks ⚖"
96
+ )
97
 
98
  with gr.Tab("Technical Analysis"):
99
  symbol_input = gr.Textbox(
 
116
  )
117
 
118
  with gr.Tab("Performance Comparison"):
119
+ with gr.Blocks():
120
  gr.Markdown("# Stock Performance Analyzer")
121
  gr.Markdown(
122
  "Enter a stock symbol and a benchmark to generate a performance report and snapshot."
 
130
  info="Some symbols may require a dot(.)suffix of corresponding exchange as TCS.NS",
131
  )
132
  benchmark_input = gr.Textbox(
133
+ label="Benchmark Symbol (e.g., ^DJI,^NSEI,^FTSE,SPY)",
134
  placeholder="Enter benchmark symbol",
135
  info="For index use (^) as that is the accepted format. It can also be other valid stocks/symbols too.",
136
  )
137
 
138
  generate_button = gr.Button("Generate Report", variant="primary")
 
139
 
 
140
  download_button = gr.File(label="Download Report")
 
141
 
142
  with gr.Row():
143
  report_output = gr.HTML(label="Performance Report")
144
 
 
 
 
 
 
 
145
  generate_button.click(
146
+ fn=get_comparison_report,
147
  inputs=[symbol_input, benchmark_input],
148
  outputs=[
 
149
  report_output,
150
  download_button,
 
151
  ],
152
  )
153