KJ commited on
Commit
7ea91b8
·
1 Parent(s): 06be36e

adding conn

Browse files
Files changed (2) hide show
  1. app.py +25 -5
  2. requirements.txt +2 -1
app.py CHANGED
@@ -2,6 +2,8 @@ import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
  from sqlalchemy import create_engine, text
 
 
5
 
6
  # --- Language toggle ---
7
  LANG = st.radio("Language / Език", ["English", "Български"], horizontal=True)
@@ -33,14 +35,32 @@ T = {
33
  }
34
  }
35
 
36
- # --- MySQL config from Hugging Face secrets ---
37
- MYSQL_HOST = st.secrets["MYSQL_HOST"]
38
- MYSQL_PORT = int(st.secrets["MYSQL_PORT"])
 
 
 
39
  MYSQL_USER = st.secrets["MYSQL_USER"]
40
  MYSQL_PASSWORD = st.secrets["MYSQL_PASSWORD"]
41
  MYSQL_DB = st.secrets["DB_NAME"]
42
-
43
- MYSQL_URL = f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DB}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  engine = create_engine(MYSQL_URL)
45
 
46
  @st.cache_data(ttl=300)
 
2
  import pandas as pd
3
  import plotly.express as px
4
  from sqlalchemy import create_engine, text
5
+ from sshtunnel import SSHTunnelForwarder
6
+ import os
7
 
8
  # --- Language toggle ---
9
  LANG = st.radio("Language / Език", ["English", "Български"], horizontal=True)
 
35
  }
36
  }
37
 
38
+ # --- SSH tunnel setup ---
39
+ SSH_KEY = st.secrets["SSH_KEY"]
40
+ SSH_USER = st.secrets["SSH_USER"]
41
+ SSH_HOST = st.secrets["DB_HOST"]
42
+ REMOTE_BIND_HOST = st.secrets["REMOTE_BIND_HOST"]
43
+ LOCAL_BIND_HOST = st.secrets["LOCAL_BIND_HOST"]
44
  MYSQL_USER = st.secrets["MYSQL_USER"]
45
  MYSQL_PASSWORD = st.secrets["MYSQL_PASSWORD"]
46
  MYSQL_DB = st.secrets["DB_NAME"]
47
+ MYSQL_PORT = st.secrets["MYSQL_PORT"]
48
+
49
+ # Write SSH key to temp file
50
+ with open("/tmp/ssh_key.pem", "w") as f:
51
+ f.write(SSH_KEY)
52
+ os.chmod("/tmp/ssh_key.pem", 0o600)
53
+
54
+ server = SSHTunnelForwarder(
55
+ (SSH_HOST, 22),
56
+ ssh_username=SSH_USER,
57
+ ssh_pkey="/tmp/ssh_key.pem",
58
+ remote_bind_address=(REMOTE_BIND_HOST, MYSQL_PORT),
59
+ local_bind_address=(LOCAL_BIND_HOST, MYSQL_PORT)
60
+ )
61
+ server.start()
62
+
63
+ MYSQL_URL = f"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{LOCAL_BIND_HOST}:{MYSQL_PORT}/{MYSQL_DB}"
64
  engine = create_engine(MYSQL_URL)
65
 
66
  @st.cache_data(ttl=300)
requirements.txt CHANGED
@@ -2,4 +2,5 @@ streamlit
2
  pandas
3
  pymysql
4
  sqlalchemy
5
- plotly
 
 
2
  pandas
3
  pymysql
4
  sqlalchemy
5
+ plotly
6
+ sshtunnel