import base64
import logging
import pathlib
import re
import sys
import uuid
import streamlit as st
import yaml
from obsei.configuration import ObseiConfiguration
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
def img_to_bytes(img_path):
img_bytes = pathlib.Path(img_path).read_bytes()
encoded = base64.b64encode(img_bytes).decode()
return encoded
# Copied from https://github.com/jrieke/traingenerator/blob/main/app/utils.py
def download_button(
object_to_download, download_filename, button_text # , pickle_it=False
):
try:
# some strings <-> bytes conversions necessary here
b64 = base64.b64encode(object_to_download.encode()).decode()
except AttributeError as e:
b64 = base64.b64encode(object_to_download).decode()
button_uuid = str(uuid.uuid4()).replace("-", "")
button_id = re.sub("\d+", "", button_uuid)
custom_css = f"""
"""
dl_link = (
custom_css
+ f'{button_text}
'
)
# dl_link = f'
'
st.markdown(dl_link, unsafe_allow_html=True)
def get_obsei_config(current_path, file_name):
return ObseiConfiguration(
config_path=current_path,
config_filename=file_name,
).configuration
@st.cache
def get_icon_name(name, icon, icon_size=40, font_size=1):
if not name:
return f''
return (
f'
' f'' f"{name}
" ) def render_config(config, component, help_str=None, parent_key=None): if config is None: return prefix = "" if parent_key is None else f"{parent_key}." if help_str is not None: with component.expander("Info", False): help_area = "\n".join(help_str) st.code(f"{help_area}") for k, v in config.items(): if k == "_target_": continue if isinstance(v, dict): render_config(v, component, None, k) elif isinstance(v, list): if len(v) == 0: continue is_object = isinstance(v[0], dict) if is_object: for idx, sub_element in enumerate(v): render_config(sub_element, component, None, f"{k}[{idx}]") else: text_data = component.text_area( f"{prefix}{k}", ", ".join(v), help="Comma separated list" ) text_list = text_data.split(",") config[k] = [text.strip() for text in text_list] elif isinstance(v, bool): options = [True, False] selected_option = component.radio(f"{prefix}{k}", options, options.index(v)) config[k] = bool(selected_option) elif k == 'jinja_template': config[k] = component.text_area( f"{prefix}{k}", v, help="Jinja Template" ) else: tokens = k.split("_") is_secret = tokens[-1] in ["key", "password", "token", "secret"] hint = ( "Enter value" if "lookup" not in tokens else "Format: `