Sai004 Moki2004 commited on
Commit
032351b
·
1 Parent(s): ec30f04

added local cache (#1)

Browse files

- added local cache (c9703f0db6f9534806027730fa0e8b90f991b4e4)


Co-authored-by: Mokshith <[email protected]>

Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -68,24 +68,32 @@ USER = os.getenv('DATABASE_USER')
68
  PASSWORD = os.getenv('DATABASE_PASSWORD')
69
  # load sql
70
 
71
-
72
  def load_sql_data(query):
73
  if SQL_KEY in CACHE:
74
  return CACHE[SQL_KEY]
75
- conn = psycopg2.connect(
76
- host=HOST,
77
- database=DATABASE,
78
- user=USER,
79
- password=PASSWORD
80
- )
81
- df = pd.read_sql_query(query, conn)
82
- df = df.drop(['item_doi'], axis=1)
83
-
84
- # Close the database connection
85
- conn.close()
86
- CACHE[SQL_KEY] = df
87
- return df
88
-
 
 
 
 
 
 
 
 
 
89
 
90
  # main_df
91
  main_df = load_sql_data(query)
 
68
  PASSWORD = os.getenv('DATABASE_PASSWORD')
69
  # load sql
70
 
 
71
  def load_sql_data(query):
72
  if SQL_KEY in CACHE:
73
  return CACHE[SQL_KEY]
74
+ try:
75
+ conn = psycopg2.connect(
76
+ host=HOST,
77
+ database=DATABASE,
78
+ user=USER,
79
+ password=PASSWORD
80
+ )
81
+ df = pd.read_sql_query(query, conn)
82
+ df = df.drop(['item_doi'], axis=1)
83
+
84
+ # Close the database connection
85
+ conn.close()
86
+
87
+ CACHE[SQL_KEY] = df
88
+ return df
89
+
90
+ except psycopg2.Error:
91
+ # If there is an error connecting to the database, load data from the compressed CSV file
92
+ df = pd.read_csv('compressed_data.bz2', compression='bz2')
93
+ df = df.drop(['item_doi'], axis=1)
94
+
95
+ CACHE[SQL_KEY] = df
96
+        return df
97
 
98
  # main_df
99
  main_df = load_sql_data(query)