Issue
I'm using sqlalchemy and psycopg to run SQL inside Python. However, there's a part of the code I want to hide when sending to other people (my passwords). Is there a way to do this?
This is how I sent to my colleagues last time, but the parts with XXXXXX were inserted by hand, I want to automate this:
import os
import sqlalchemy
pd.options.display.max_columns = 500
pd.options.display.max_rows = 500
from datetime import timedelta
import psycopg2
import csv
DB_USER = 'XXXXXX'
DB_PASS = 'XXXXXX'
DB_HOST = 'XXXXXX'
DB_PORT = XXXXXX
DB_NAME = 'xxxxxx'
def get_engine():
return sqlalchemy.create_engine(
'postgresql://{}:{}@{}:{}/{}'.format(DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME))
def run_query(query_txt):
engine = get_engine()
with engine.begin() as con:
return pd.read_sql(query_txt, con)
I found some related posts, like hide code in jupyter notebook and Hide selected code cells in Jupyter Notebook, but they didn't answer to my question.
Solution
Not sure if it is the appropriate answer, but you may store the part in another .py
file under the same directory, and call it every time. For example, the file with name name.py
can look like:
DB_USER = 'asdf'
DB_PASS = 'yxc'
DB_HOST = 'asdf'
DB_PORT = 23
DB_NAME = 'sdfdsfds'
In your current jupyter notebook, you can safely delete these lines but access the variables by:
from name import *
DB_USER
gives you:
'asdf'
Answered By - tianlinhe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.