Skip to content

Instantly share code, notes, and snippets.

@dormeir999
Last active June 10, 2022 14:21
Show Gist options
  • Save dormeir999/17296322cdfcad9b95ed869f81dc8b6d to your computer and use it in GitHub Desktop.
Save dormeir999/17296322cdfcad9b95ed869f81dc8b6d to your computer and use it in GitHub Desktop.
Stremalit_Embedding_Jupyter
# Helper functions
def get_jupyter_configs(file_name=None):
if file_name == None:
file_name = os.path.expanduser(os.path.join(("~"), ".jupyter", "jupyter_notebook_config.py"))
with open(file_name) as f:
lines = f.readlines()
relevant_lines = [line.replace("\n", "") for line in lines if
not line.lstrip().startswith("#") and not line == '\n']
dict_lines = [line for line in relevant_lines if "=" not in line]
# Find every dictionary that lasts for more than one line, and consolidate it into one line
if len(dict_lines) > 0:
dict_lines_joined = [line + "}}" for line in "".join(dict_lines).split("}}")[:-1]]
relevant_lines = [line for line in relevant_lines if "=" in line]
indices = [i for i, x in enumerate(relevant_lines) if "{" in x]
for j, i in enumerate(indices):
relevant_lines[i] = relevant_lines[i] + dict_lines_joined[j]
return {line.split("=")[0].strip(): line.split("=")[1].strip().replace("\n", "") for line in relevant_lines if
"=" in
line and "#" not in line}
def get_jupyter_dir(jupyter_configs=None):
if not jupyter_configs:
jupyter_configs = get_jupyter_configs()
folder = jupyter_configs['c.NotebookApp.notebook_dir']
return folder if not folder.startswith("r") else folder[1:].replace("'", "")
def get_jupyter_port(jupyter_configs=None):
if not jupyter_configs:
jupyter_configs = get_jupyter_configs()
return int(jupyter_configs['c.NotebookApp.tornado_settings'].split("http://")[-1].split(":")[1].split("/")[0]) + 1
def get_jupyter_ip(jupyter_configs=None):
if not jupyter_configs:
jupyter_configs = get_jupyter_configs()
return jupyter_configs['c.NotebookApp.tornado_settings'].split("http://")[-1].split(":")[0]
def def_jupyter_state_variables(state):
state.jupyter_configs = state.jupyter_configs if state.jupyter_configs else get_jupyter_configs()
state.jupyter_ip = state.jupyter_ip if state.jupyter_ip else get_jupyter_ip(state.jupyter_configs)
state.jupyter_port = state.jupyter_port if state.jupyter_port else get_jupyter_port(state.jupyter_configs)
state.jupyter_dir = state.jupyter_dir if state.jupyter_dir else get_jupyter_dir(state.jupyter_configs)
def is_jupyter_up():
return is_port_up(get_jupyter_port())
def is_port_up(port):
location = ("127.0.0.1", port)
a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if a_socket.connect_ex(location) == 0:
return True
else:
return False
# Implementation
notebook_message = configs['JUPYTER_NOTEBOOK_STR']
lab_message = configs['JUPYTER_LAB_STR']
coding_phase = [notebook_message, lab_message]
state.coding_phase = st.radio("", options=coding_phase,
index=coding_phase.index(state.coding_phase) if state.coding_phase else 0)
def_jupyter_state_variables(state)
jupyter_lab = f"http://{state.jupyter_ip}:{state.jupyter_port}"
list_of_files = \
[os.path.join(state.jupyter_dir, file) for file in os.listdir(state.jupyter_dir) if file.endswith(".ipynb")]
notebook_file = os.path.basename(max(list_of_files, key=os.path.getmtime))
jupyter_notebook = f"http://{state.jupyter_ip}:{state.jupyter_port}/notebooks/{notebook_file}"
jupyter_notebook_dir = state.jupyter_configs["c.NotebookApp.notebook_dir"].replace("r'","").replace("'","")
jupyter_notebook_dir = rf"{jupyter_notebook_dir}"
if not os.path.exists(jupyter_notebook_dir):
return st.info(f"Jupyter home dir - {jupyter_notebook_dir} - doesn't exist")
waiting_message = st.empty()
while not is_jupyter_up():
time.sleep(0)
jup_width = 1500
jup_height = 1000
linespace_generator(2)
waiting_message.warning("Loading Jupyter")
if state.coding_phase == lab_message:
components.iframe(jupyter_lab, width=jup_width, height=jup_height)
elif state.coding_phase == notebook_message:
components.iframe(jupyter_notebook, width=jup_width, height=jup_height)
time.sleep(2)
waiting_message.empty()
@hadsed
Copy link

hadsed commented Jun 10, 2022

i think you're missing the definition of state (i assume it's just streamlit.session_state?)

(btw, super helpful on the jupyter config to make this work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment