From 38cb5fb623056218aff874c8a4dd196735a5ac21 Mon Sep 17 00:00:00 2001 From: Lucca Ketterer Date: Sat, 21 Jan 2023 16:05:11 +0100 Subject: [PATCH] add experimental streamlit integration --- controller/controller.py | 37 +++++++++++++-- controller/gui.py | 99 ++++++++++++++++++++++++++++------------ 2 files changed, 103 insertions(+), 33 deletions(-) diff --git a/controller/controller.py b/controller/controller.py index 68513b8..28b45c3 100755 --- a/controller/controller.py +++ b/controller/controller.py @@ -81,15 +81,42 @@ def set_pixels(color): # @click.option("-v", help="Set HEX Color as base visualizer color: -v ffffff") def main(arg): if arg == (): - proc = subprocess.Popen(["python", "-m", "streamlit", "run", "/var/lib/lc/gui.py", "--server.headless", "True", "--theme.base", "dark", "--theme.primaryColor", "#9f0000", "--theme.backgroundColor", "#181a1b", "--theme.secondaryBackgroundColor", "#1f2123", "--theme.textColor", "#c4c0b8"], stdout=subprocess.PIPE) - for line in proc.stdout: - if line == b' You can now view your Streamlit app in your browser.\n': - break + # proc = subprocess.Popen(["python", "-m", "streamlit", "run", "/var/lib/lc/gui.py", "--server.headless", "True", "--theme.base", "dark", "--theme.primaryColor", "#9f0000", "--theme.backgroundColor", "#181a1b", "--theme.secondaryBackgroundColor", "#1f2123", "--theme.textColor", "#c4c0b8"], stdout=subprocess.PIPE) + # for line in proc.stdout: + # if line == b' You can now view your Streamlit app in your browser.\n': + # break + + import streamlit.web.bootstrap as bootstrap + from streamlit import config + import gui as gui + + di = {'global_disableWatchdogWarning': None, 'global_showWarningOnDirectExecution': None, 'global_developmentMode': None, 'global_logLevel': None, 'global_unitTest': None, 'global_suppressDeprecationWarnings': None, 'global_minCachedMessageSize': None, 'global_maxCachedMessageAge': None, 'global_dataFrameSerialization': None, 'logger_level': None, 'logger_messageFormat': None, 'logger_enableRich': None, 'client_caching': None, 'client_displayEnabled': None, 'client_showErrorDetails': None, 'runner_magicEnabled': None, 'runner_installTracer': None, 'runner_fixMatplotlib': None, 'runner_postScriptGC': None, 'runner_fastReruns': None, 'server_folderWatchBlacklist': None, 'server_fileWatcherType': None, 'server_cookieSecret': None, 'server_headless': True, 'server_runOnSave': None, 'server_allowRunOnSave': None, 'server_address': None, 'server_port': None, 'server_scriptHealthCheckEnabled': None, 'server_baseUrlPath': None, 'server_enableCORS': None, 'server_enableXsrfProtection': None, 'server_maxUploadSize': None, 'server_maxMessageSize': None, 'server_enableWebsocketCompression': None, 'browser_serverAddress': None, 'browser_gatherUsageStats': None, 'browser_serverPort': None, 'ui_hideTopBar': None, 'ui_hideSidebarNav': None, 'mapbox_token': None, 'deprecation_showfileUploaderEncoding': None, 'deprecation_showImageFormat': None, 'deprecation_showPyplotGlobalUse': None, 'theme_base': None, 'theme_primaryColor': None, 'theme_backgroundColor': None, 'theme_secondaryBackgroundColor': None, 'theme_textColor': None, 'theme_font': None} + + import multiprocessing + + def run(): + config.set_option('server.headless', True) + bootstrap.run(gui.__file__, 'streamlit run gui.py --server.headless True', [], di) + + p = multiprocessing.Process(target=run) + p.start() + + import requests + + while True: + try: + print(requests.get('http://localhost:8501')) + except: + continue + print('asdf') + break + # cli.main_run(str(gui.__file__)) webview.create_window('LED Control', 'http://localhost:8501') webview.start() - proc.terminate() + # proc.terminate() exit() + match arg[0]: case "help": print("lc [help|set|search|list]") diff --git a/controller/gui.py b/controller/gui.py index b464b9d..47140fc 100644 --- a/controller/gui.py +++ b/controller/gui.py @@ -1,33 +1,76 @@ -import streamlit as st -import controller -import colorsys -hide_streamlit_style = """ - -""" -st.markdown(hide_streamlit_style, unsafe_allow_html=True) +def main(): + import streamlit as st + import controller + import colorsys + import pickle + import os + from PIL import Image -def hex_to_rgb(hex): - r = int(hex[1:3],16) - g = int(hex[3:5],16) - b = int(hex[5:7],16) - return r,g,b + hide_streamlit_style = """ + -def rgb_to_hex(r,g,b): - return "#" + hex(r)[2:].zfill(2) + hex(g)[2:].zfill(2) + hex(b)[2:].zfill(2) + """ + st.markdown(hide_streamlit_style, unsafe_allow_html=True) -st.title("LED Control") + def hex_to_rgb(hex): + r = int(hex[1:3],16) + g = int(hex[3:5],16) + b = int(hex[5:7],16) + return r,g,b -# r = st.slider("Red", 0, 255, 0) -# g = st.slider("Green", 0, 255, 0) -# b = st.slider("Blue", 0, 255, 0) -h = st.slider("Hue", 0.0, 1.0, 0.0, 0.01) -s = st.slider("Saturation", 0.0, 1.0, 0.0, 0.01) -v = st.slider("Value", 0.0, 1.0, 0.0, 0.01) -r,g,b = colorsys.hsv_to_rgb(h,s,v) -color = rgb_to_hex(int(r * 255),int(g * 255),int(b * 255)) -controller.set_pixels(color[1:]) -color + def rgb_to_hex(r,g,b): + return "#" + hex(r)[2:].zfill(2) + hex(g)[2:].zfill(2) + hex(b)[2:].zfill(2) + + def load_config(): + try: + path = os.path.expanduser("~/.config/lc/lc.dmp") + with open(path, 'rb') as f: + return pickle.load(f) + except FileNotFoundError: + return {'color': '000000', 'saved': []} + + + def save(conf): + path = os.path.expanduser("~/.config/lc/lc.dmp") + with open(path, 'wb') as f: + pickle.dump(conf, f) + + def add_to_conf(): + st.session_state.config['saved'].append(color) + + init = 0 + if 'config' not in st.session_state: + init = 1 + st.session_state.config = load_config() + color = st.session_state.config['color'] + + st.title("LED Control") + + # r = st.slider("Red", 0, 255, 0) + # g = st.slider("Green", 0, 255, 0) + # b = st.slider("Blue", 0, 255, 0) + h = st.slider("Hue", 0.0, 1.0, 0.0, 0.01) + s = st.slider("Saturation", 0.0, 1.0, 0.0, 0.01) + v = st.slider("Value", 0.0, 1.0, 0.0, 0.01) + if init == 0: + r,g,b = colorsys.hsv_to_rgb(h,s,v) + color = rgb_to_hex(int(r * 255),int(g * 255),int(b * 255)) + + + st.session_state.config['color'] = color[1:] + save(st.session_state.config) + controller.set_pixels(color[1:]) + img = Image.new(mode="RGB", size=(30,30), color=(int(r * 255),int(g * 255),int(b * 255))) + + st.subheader(color) + st.image(img) + st.button("Save Color", add_to_conf) + st.session_state.config['saved'] + + +if __name__ == "__main__": + main()