-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_server.py
More file actions
46 lines (38 loc) · 1.24 KB
/
socket_server.py
File metadata and controls
46 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
from websockets.server import serve
import pandas as pd
import sqlite3
import pickle
from constants import *
conn = sqlite3.connect('analysis.sqlite')
# sql_query = f'SELECT * FROM {tables[0]["table"]}'
async def echo(websocket):
query = ""
async for message in websocket:
if message:
print("query received: ", message)
query = message
break
else:
print("No query received")
break
pickled_df = None
try:
# parsed_query = pd.read_sql_query(query if query else sql_query, conn)
# df = pd.DataFrame(parsed_query, columns=tables[0]["columns"])
# print(df.shape)
# print("query: ", query if query else sql_query)
#TODO: fetch from db
type_df = pd.read_csv("data/type_data.csv")
total_df = pd.read_csv("data/total_data.csv").iloc[:,1:]
print(type_df, total_df)
# df = pd.read_csv("output.csv")
pickled_df = pickle.dumps([type_df, total_df])
except Exception as e:
print("Exception", e)
finally:
await websocket.send(pickled_df)
async def main():
async with serve(echo,'', socket_port):
await asyncio.Future()
asyncio.run(main())