eshan6704 commited on
Commit
8bce80c
·
verified ·
1 Parent(s): 1af41e5

Update app/ui_html.py

Browse files
Files changed (1) hide show
  1. app/ui_html.py +74 -140
app/ui_html.py CHANGED
@@ -1,164 +1,98 @@
1
- # ui_html.py
2
- from . import screener
3
-
4
- def build_frontend_html():
5
- return """
6
  <!DOCTYPE html>
7
- <html lang="en">
8
  <head>
9
- <meta charset="UTF-8">
10
- <title>Stock / Index Backend UI</title>
11
-
12
- <style>
13
- body {
14
- font-family: Arial, sans-serif;
15
- margin: 0;
16
- padding: 10px;
17
- }
18
- .controls {
19
- display: flex;
20
- flex-wrap: wrap;
21
- gap: 10px;
22
- }
23
- select, input, button {
24
- padding: 6px;
25
- font-size: 14px;
26
- }
27
- #response {
28
- margin-top: 15px;
29
- padding: 10px;
30
- border: 1px solid #ccc;
31
- height: calc(100vh - 120px);
32
- overflow: auto;
33
- }
34
- </style>
35
  </head>
36
-
37
  <body>
38
 
39
- <h2>Stock / Index Fetcher</h2>
 
 
 
 
40
 
41
- <div class="controls">
42
- <select id="mode">
43
- <option value="stock">stock</option>
44
- <option value="index">index</option>
45
- <option value="screener">screener</option>
46
- </select>
47
 
48
- <select id="req_type"></select>
49
- <select id="name"></select>
50
 
51
- <input id="date_start" placeholder="YYYY-MM-DD">
52
- <input id="date_end" placeholder="YYYY-MM-DD">
53
 
54
- <button onclick="fetchData()">Fetch</button>
55
- </div>
56
-
57
- <div id="response">Loading req_type list...</div>
58
 
59
  <script>
60
- const API_URL = "/api/fetch";
61
- let REQ_MAP = {};
62
-
63
- /* ---------------- LOAD LIST ---------------- */
64
- async function loadReqTypes() {
65
- const res = await fetch(API_URL, {
66
- method: "POST",
67
- headers: {"Content-Type":"application/json"},
68
- body: JSON.stringify({ mode: "list" })
69
- });
70
-
71
- const html = await res.text();
72
- const doc = new DOMParser().parseFromString(html, "text/html");
73
-
74
- REQ_MAP = { stock: [], index: [], screener: [] };
75
-
76
- doc.querySelectorAll("li").forEach(li => {
77
- const mode = li.dataset.mode;
78
- const type = li.dataset.type;
79
- const names = (li.dataset.names || "").split(",").filter(Boolean);
80
-
81
- if (mode && type) {
82
- REQ_MAP[mode].push({ type, names });
83
- }
84
  });
85
-
86
- updateReqType();
87
- }
88
-
89
- /* ---------------- REQ TYPE ---------------- */
90
- function updateReqType() {
91
- const mode = document.getElementById("mode").value;
92
- const sel = document.getElementById("req_type");
93
- sel.innerHTML = "";
94
-
95
- REQ_MAP[mode].forEach(r => {
96
- const o = document.createElement("option");
97
- o.value = r.type;
98
- o.textContent = r.type;
99
- sel.appendChild(o);
100
- });
101
-
102
- if (mode === "stock") sel.value = "info";
103
- if (mode === "index") sel.value = "indices";
104
- if (mode === "screener") sel.value = "from-high";
105
-
106
- updateName();
107
  }
108
 
109
- /* ---------------- NAME ---------------- */
110
- function updateName() {
111
- const mode = document.getElementById("mode").value;
112
- const rt = document.getElementById("req_type").value;
113
- const sel = document.getElementById("name");
114
- sel.innerHTML = "";
115
-
116
- const entry = REQ_MAP[mode].find(r => r.type === rt);
117
-
118
- if (entry && entry.names.length) {
119
- entry.names.forEach(n => {
120
- const o = document.createElement("option");
121
- o.value = n;
122
- o.textContent = n;
123
- sel.appendChild(o);
124
- });
125
- } else {
126
- const o = document.createElement("option");
127
- o.value = "";
128
- o.textContent = "-- none --";
129
- sel.appendChild(o);
130
- }
131
-
132
- if (mode === "stock") sel.value = "ITC";
133
- if (mode === "index") sel.value = "NIFTY 50";
134
  }
135
 
136
- /* ---------------- FETCH ---------------- */
137
- async function fetchData() {
138
- const payload = {
139
- mode: mode.value,
140
- req_type: req_type.value,
141
- name: name.value,
142
- date_start: date_start.value,
143
- date_end: date_end.value
144
- };
145
-
146
- const res = await fetch(API_URL, {
147
- method: "POST",
148
- headers: {"Content-Type":"application/json"},
149
- body: JSON.stringify(payload)
150
  });
151
-
152
- document.getElementById("response").innerHTML = await res.text();
 
153
  }
154
 
155
- /* EVENTS */
156
- mode.addEventListener("change", updateReqType);
157
- req_type.addEventListener("change", updateName);
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
- loadReqTypes();
 
 
160
  </script>
161
-
162
  </body>
163
  </html>
164
  """
 
1
+ HTML = """
 
 
 
 
2
  <!DOCTYPE html>
3
+ <html>
4
  <head>
5
+ <meta charset="utf-8">
6
+ <title>Stock / Index UI</title>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  </head>
 
8
  <body>
9
 
10
+ <select id="mode">
11
+ <option value="stock">stock</option>
12
+ <option value="index">index</option>
13
+ <option value="screener">screener</option>
14
+ </select>
15
 
16
+ <select id="req_type"></select>
17
+ <select id="name"></select>
 
 
 
 
18
 
19
+ <input id="date_start" placeholder="dd-mm-yyyy">
20
+ <input id="date_end" placeholder="dd-mm-yyyy">
21
 
22
+ <button onclick="fetchData()">Fetch</button>
 
23
 
24
+ <div id="response">Loading...</div>
 
 
 
25
 
26
  <script>
27
+ const API="/api/fetch";
28
+ let META={stock:[],index:[],screener:[]};
29
+
30
+ async function init(){
31
+ const r=await fetch(API,{
32
+ method:"POST",
33
+ headers:{"Content-Type":"application/json"},
34
+ body:JSON.stringify({mode:"list"})
35
+ });
36
+ const html=await r.text();
37
+ const doc=new DOMParser().parseFromString(html,"text/html");
38
+
39
+ doc.querySelectorAll("li").forEach(li=>{
40
+ META[li.dataset.mode].push({
41
+ type:li.dataset.type,
42
+ names:(li.dataset.names||"").split(",").filter(Boolean)
 
 
 
 
 
 
 
 
43
  });
44
+ });
45
+ updateReq();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  }
47
 
48
+ function updateReq(){
49
+ req_type.innerHTML="";
50
+ META[mode.value].forEach(x=>{
51
+ let o=document.createElement("option");
52
+ o.value=x.type;o.text=x.type;
53
+ req_type.appendChild(o);
54
+ });
55
+
56
+ if(mode.value==="stock") req_type.value="info";
57
+ if(mode.value==="index") req_type.value="indices";
58
+ if(mode.value==="screener") req_type.value="from-high";
59
+ updateName();
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  }
61
 
62
+ function updateName(){
63
+ name.innerHTML="";
64
+ const r=META[mode.value].find(x=>x.type===req_type.value);
65
+ if(r && r.names.length){
66
+ r.names.forEach(n=>{
67
+ let o=document.createElement("option");
68
+ o.value=n;o.text=n;
69
+ name.appendChild(o);
 
 
 
 
 
 
70
  });
71
+ } else {
72
+ name.innerHTML="<option></option>";
73
+ }
74
  }
75
 
76
+ async function fetchData(){
77
+ const p={
78
+ mode:mode.value,
79
+ req_type:req_type.value,
80
+ name:name.value,
81
+ date_start:date_start.value,
82
+ date_end:date_end.value
83
+ };
84
+ const r=await fetch(API,{
85
+ method:"POST",
86
+ headers:{"Content-Type":"application/json"},
87
+ body:JSON.stringify(p)
88
+ });
89
+ response.innerHTML=await r.text();
90
+ }
91
 
92
+ mode.onchange=updateReq;
93
+ req_type.onchange=updateName;
94
+ init();
95
  </script>
 
96
  </body>
97
  </html>
98
  """