From 20a83e281ed208a3948fcecb2dd2671d5301c7f0 Mon Sep 17 00:00:00 2001
From: Julian Rasch <julian.rasch@gmail.com>
Date: Thu, 27 Feb 2025 10:32:40 +0100
Subject: [PATCH] switched app back to dash

---
 Dockerfile                               | 15 ++--
 app/app.py                               | 94 ++++++++++++++----------
 app/callbacks.py                         | 65 ++++++++++++++++
 app/layout.py                            | 40 ++++++++++
 app/my_app.py                            | 73 +++++++++++++++---
 app/streamlit-favicon.svg                |  5 --
 dash_proxy/dash_proxy.py                 | 16 ++++
 dash_proxy/my_app_proxy.py               | 16 ++++
 {streamlit_proxy => dash_proxy}/setup.py | 10 +--
 my_callbacks.py                          | 21 ++++++
 my_layout.py                             | 13 ++++
 requirements.txt                         |  7 +-
 streamlit_app.py                         |  4 -
 streamlit_proxy/my_app_proxy.py          | 21 ------
 streamlit_proxy/streamlit_proxy.py       | 25 -------
 15 files changed, 306 insertions(+), 119 deletions(-)
 create mode 100644 app/callbacks.py
 create mode 100644 app/layout.py
 delete mode 100644 app/streamlit-favicon.svg
 create mode 100644 dash_proxy/dash_proxy.py
 create mode 100644 dash_proxy/my_app_proxy.py
 rename {streamlit_proxy => dash_proxy}/setup.py (55%)
 create mode 100644 my_callbacks.py
 create mode 100644 my_layout.py
 delete mode 100644 streamlit_app.py
 delete mode 100644 streamlit_proxy/my_app_proxy.py
 delete mode 100644 streamlit_proxy/streamlit_proxy.py

diff --git a/Dockerfile b/Dockerfile
index 3e4f965..d9231e2 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -7,9 +7,9 @@ RUN pip install --no-cache-dir -r requirements.txt && rm requirements.txt
 
 ENV HOME_PATH=/home/jovyan/
 
-# add the proxy for streamlit
-COPY streamlit_proxy /tmp/streamlit_proxy/
-RUN pip install /tmp/streamlit_proxy/
+# add the proxy for dash
+COPY dash_proxy /tmp/dash_proxy/
+RUN pip install /tmp/dash_proxy/
 
 # install some utilities for GPT
 COPY llm_utils /llm_utils/
@@ -17,12 +17,7 @@ RUN pip install /llm_utils/
 ENV CONFIG_PATH=/home/jovyan/config.txt
 
 # copy the apps into the container
-COPY app /streamlit/app/
-RUN chown -R jovyan /streamlit/app/
-
-# install some NLTK and spaCy data
-RUN python -m nltk.downloader stopwords
-RUN python -m nltk.downloader wordnet
-RUN python -m nltk.downloader punkt
+COPY app /dash/app/
+RUN chown -R jovyan /dash/app/
 
 USER jovyan
\ No newline at end of file
diff --git a/app/app.py b/app/app.py
index 66b5c96..b43ae3a 100644
--- a/app/app.py
+++ b/app/app.py
@@ -1,49 +1,65 @@
-import os
-import streamlit as st
+import sys 
+sys.path.append("/home/jovyan/")
 
-from llm_utils.client import get_openai_client
+import argparse
+import logging
 
+from urllib.parse import urlparse, urljoin
 
-MODEL = "gpt-4o"
-client = get_openai_client(
-    model=MODEL,
-    config_path=os.environ.get("CONFIG_PATH")
-)
+from dash import Dash
+
+from jupyter_server.serverapp import list_running_servers
+
+from layout import layout
+from callbacks import register_callbacks
+
+logging.basicConfig(level=logging.INFO)
+
+# weird trick to find base_url for the jupyterlab
+def find_jupyterlab_base_url():
+    servers = list_running_servers()
+    for server in servers:
+        if server["port"] == 8888:
+            return server['url']
+    return None
 
 
+# get the correct port from proxy
+parser = argparse.ArgumentParser()
+parser.add_argument("--port", type=int)
+args = parser.parse_args()
+port: int = args.port
 
-# STREAMLIT APP
+if not port:
+    raise ValueError(f"Port of proxy server for Dash not found in {args}.")
+else: 
+    logging.debug(f"Dash app running on port {port}.")
 
-st.title("ChatGPT in Streamlit")
 
-client = get_openai_client(
-    model=MODEL,
-    config_path=os.environ.get("CONFIG_PATH")
+base_url = find_jupyterlab_base_url()
+if base_url is None:
+    raise ValueError("Base URL of Jupyterlab could not be detected.")
+logging.debug(f"Base URL: {base_url}")
+
+proxy_base_path = urlparse(urljoin(base_url + "/", f"proxy/{port}/")).path
+logging.debug(f"Proxy base path: {proxy_base_path}")
+
+# define Dash app
+app = Dash(
+    name=__name__, 
+    requests_pathname_prefix=proxy_base_path
 )
 
-if "openai_model" not in st.session_state:
-    st.session_state["openai_model"] = "gpt-4o"
-
-if "messages" not in st.session_state:
-    st.session_state.messages = []
-
-for message in st.session_state.messages:
-    with st.chat_message(message["role"]):
-        st.markdown(message["content"])
-
-if prompt := st.chat_input("What is up?"):
-    st.session_state.messages.append({"role": "user", "content": prompt})
-    with st.chat_message("user"):
-        st.markdown(prompt)
-
-    with st.chat_message("assistant"):
-        stream = client.chat.completions.create(
-            model=st.session_state["openai_model"],
-            messages=[
-                {"role": m["role"], "content": m["content"]}
-                for m in st.session_state.messages
-            ],
-            stream=True,
-        )
-        response = st.write_stream(stream)
-    st.session_state.messages.append({"role": "assistant", "content": response})
\ No newline at end of file
+# define layout
+app.layout = layout
+
+# register all callback functions
+register_callbacks(app=app)
+
+# Run Dash app in the notebook
+app.run(
+    jupyter_mode="jupyterlab", 
+    port=port, 
+    host="0.0.0.0",
+    debug=True
+)
diff --git a/app/callbacks.py b/app/callbacks.py
new file mode 100644
index 0000000..2074195
--- /dev/null
+++ b/app/callbacks.py
@@ -0,0 +1,65 @@
+import os
+from datetime import datetime
+
+from dash import (
+    html, 
+    Dash
+)
+from dash.dependencies import (
+    Input, 
+    Output, 
+    State
+)
+
+from llm_utils.client import ChatGPT, get_openai_client
+
+
+def format_chat_messages(chat_history):
+    chat_messages = []
+    for message in chat_history:
+        chat_messages.append(html.Div([
+            html.P(f'{message["sender"]}: {message["message"]}'),
+            html.P(f'Sent at: {message["timestamp"]}')
+        ]))
+    return chat_messages
+
+
+def register_callbacks(app: Dash):
+    model="gpt-4o"
+    client = get_openai_client(
+        model=model,
+        config_path=os.environ.get("CONFIG_PATH")
+    )
+    chat_gpt = ChatGPT(
+        client=client,
+        model=model
+    )
+    
+    @app.callback(
+        [Output('chat-container', 'children'),
+        Output('chat-history', 'data')],
+        [Input('send-button', 'n_clicks')],
+        [State('user-input', 'value'),
+        State('chat-history', 'data')]
+    )
+    def update_chat(n_clicks, input_value, chat_history):
+        if chat_history is None:
+            chat_history = []
+        
+        if n_clicks > 0 and input_value:
+            chat_history.append({
+                'sender': 'User',
+                'message': input_value,
+                'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+            })
+            
+            response = chat_gpt.chat_with_gpt(input_value)
+            
+            # Add response to chat history
+            chat_history.append({
+                'sender': 'Language Model',
+                'message': response,
+                'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+            })
+        
+        return format_chat_messages(chat_history), chat_history
diff --git a/app/layout.py b/app/layout.py
new file mode 100644
index 0000000..4d82a24
--- /dev/null
+++ b/app/layout.py
@@ -0,0 +1,40 @@
+from dash import (
+    html, 
+    dcc
+)
+
+layout = html.Div([
+    dcc.Store(
+        id='chat-history', 
+        data=[]
+    ),
+    html.H1(
+        "Simple Chat App", 
+        style={'text-align': 'center'}
+    ),
+    html.Div(
+        id='chat-container', 
+        style={'overflowY': 'scroll', 'height': '70vh', 'padding': '10px'}
+    ),
+    html.Div([
+        dcc.Input(
+            id='user-input', 
+            type='text', 
+            placeholder='Type your message...', 
+            debounce=True
+        ),
+        html.Button(
+            'Send', 
+            id='send-button', 
+            n_clicks=0
+        )
+    ], style={
+        'display': 'flex', 
+        'alignItems': 'center', 
+        'justifyContent': 'center', 
+        'position': 'fixed', 
+        'bottom': 0, 
+        'width': '100%', 
+        'padding': '10px'
+    })
+], style={'position': 'relative'})
diff --git a/app/my_app.py b/app/my_app.py
index 7d203b4..db083be 100644
--- a/app/my_app.py
+++ b/app/my_app.py
@@ -1,15 +1,70 @@
-import os
 import sys 
+sys.path.append("/home/jovyan/")
 
-HOME_PATH = os.environ.get("HOME_PATH")
-sys.path.append(HOME_PATH)
+import argparse
+import logging
 
-print(HOME_PATH)
+from urllib.parse import urlparse, urljoin
 
-try: 
-    import streamlit_app # this runs the app
+from dash import Dash
+
+from jupyter_server.serverapp import list_running_servers
 
-except ModuleNotFoundError as e:
-    # do not let start
-    print(e)
+try: 
+    from my_layout import layout
+    from my_callbacks import register_callbacks
+except ModuleNotFoundError:
+    # do not let Dash start
     exit()
+
+
+logging.basicConfig(level=logging.INFO)
+
+# weird trick to find base_url for the jupyterlab
+def find_jupyterlab_base_url():
+    servers = list_running_servers()
+    for server in servers:
+        if server["port"] == 8888:
+            return server['url']
+    return None
+
+
+# get the correct port from proxy
+parser = argparse.ArgumentParser()
+parser.add_argument("--port", type=int)
+args = parser.parse_args()
+port: int = args.port
+
+if not port:
+    raise ValueError(f"Port of proxy server for Dash not found in {args}.")
+else: 
+    logging.debug(f"Dash app running on port {port}.")
+
+
+base_url = find_jupyterlab_base_url()
+if base_url is None:
+    raise ValueError("Base URL of Jupyterlab could not be detected.")
+logging.debug(f"Base URL: {base_url}")
+
+proxy_base_path = urlparse(urljoin(base_url + "/", f"proxy/{port}/")).path
+logging.debug(f"Proxy base path: {proxy_base_path}")
+
+# define Dash app
+app = Dash(
+    name=__name__, 
+    requests_pathname_prefix=proxy_base_path
+)
+
+# define layout
+app.layout = layout
+
+# register all callback functions
+register_callbacks(app=app)
+
+# Run Dash app in the notebook
+app.run(
+    jupyter_mode="jupyterlab", 
+    port=port, 
+    host="0.0.0.0",
+    debug=True
+)
diff --git a/app/streamlit-favicon.svg b/app/streamlit-favicon.svg
deleted file mode 100644
index f676ed8..0000000
--- a/app/streamlit-favicon.svg
+++ /dev/null
@@ -1,5 +0,0 @@
-<svg width="512" height="512" viewBox="0 0 512 512" fill="none" xmlns="http://www.w3.org/2000/svg">
-<path d="M255.968 288.494L166.211 241.067L10.4062 158.753C10.2639 158.611 9.97951 158.611 9.83728 158.611C4.14838 155.909 -1.68275 161.596 0.450591 167.283L79.8393 369.685L79.8535 369.728C79.9388 369.927 80.0099 370.126 80.0953 370.325C83.3522 377.874 90.4633 382.537 98.2002 384.371C98.8544 384.513 99.3225 384.643 100.108 384.799C100.89 384.973 101.983 385.21 102.922 385.281C103.078 385.295 103.221 385.295 103.377 385.31H103.491C103.605 385.324 103.718 385.324 103.832 385.338H103.989C104.088 385.352 104.202 385.352 104.302 385.352H104.486C104.6 385.366 104.714 385.366 104.828 385.366L167.175 392.161C226.276 398.602 285.901 398.602 345.002 392.161L407.35 385.366C408.558 385.366 409.739 385.31 410.877 385.196C411.246 385.153 411.602 385.111 411.958 385.068C412 385.054 412.057 385.054 412.1 385.039C412.342 385.011 412.583 384.968 412.825 384.926C413.181 384.883 413.536 384.812 413.892 384.741C414.603 384.585 414.926 384.471 415.891 384.139C416.856 383.808 418.458 383.228 419.461 382.745C420.464 382.261 421.159 381.798 421.999 381.272C423.037 380.618 424.024 379.948 425.025 379.198C425.457 378.868 425.753 378.656 426.066 378.358L425.895 378.258L255.968 288.494Z" fill="#FF2B2B"/>
-<path d="M501.789 158.755H501.647L345.784 241.07L432.426 370.058L511.616 167.285V167.001C513.607 161.03 507.492 155.627 501.789 158.755" fill="#7D353B"/>
-<path d="M264.274 119.615C260.292 113.8 251.616 113.8 247.776 119.615L166.211 241.068L255.968 288.495L426.067 378.357C427.135 377.312 427.991 376.293 428.897 375.217C430.177 373.638 431.372 371.947 432.424 370.056L345.782 241.068L264.274 119.615Z" fill="#BD4043"/>
-</svg>
diff --git a/dash_proxy/dash_proxy.py b/dash_proxy/dash_proxy.py
new file mode 100644
index 0000000..309e977
--- /dev/null
+++ b/dash_proxy/dash_proxy.py
@@ -0,0 +1,16 @@
+def setup_dash_proxy():
+    command = [
+        'python',
+        '/dash/app/app.py',
+        '--port',
+        '{port}'
+    ]
+    
+    return {
+        "command": command,
+        "new_browser_tab": False,
+        "launcher_entry": {
+            "enabled": True,
+            'title': 'Dash'
+        }
+    }
diff --git a/dash_proxy/my_app_proxy.py b/dash_proxy/my_app_proxy.py
new file mode 100644
index 0000000..e91a864
--- /dev/null
+++ b/dash_proxy/my_app_proxy.py
@@ -0,0 +1,16 @@
+def setup_my_app_proxy():
+    command = [
+        'python',
+        '/dash/app/my_app.py',
+        '--port',
+        '{port}'
+    ]
+    
+    return {
+        "command": command,
+        "new_browser_tab": False,
+        "launcher_entry": {
+            "enabled": True,
+            'title': 'MyApp'
+        }
+    }
diff --git a/streamlit_proxy/setup.py b/dash_proxy/setup.py
similarity index 55%
rename from streamlit_proxy/setup.py
rename to dash_proxy/setup.py
index a2dc2a9..437a268 100644
--- a/streamlit_proxy/setup.py
+++ b/dash_proxy/setup.py
@@ -3,14 +3,14 @@ import setuptools
 setuptools.setup(
     author="Julian Rasch",
     author_email="julian.rasch@fh-muenster.de",
-    description="A small module to run Streamlit inside a dockerized JupyterLab.",
-    name="jupyter-streamlit-proxy",
-    py_modules=["streamlit_proxy", "my_app_proxy"],
+    description="A small module to run Dash inside a dockerized Jupyterlab.",
+    name="jupyter-dash-proxy",
+    py_modules=["dash_proxy", "my_app_proxy"],
     entry_points={
         "jupyter_serverproxy_servers": [
-            "Streamlit = streamlit_proxy:setup_streamlit_proxy",
+            "Dash = dash_proxy:setup_dash_proxy",
             "MyApp = my_app_proxy:setup_my_app_proxy"
         ]
     },
     install_requires=["jupyter-server-proxy==4.4.0"],
-)
\ No newline at end of file
+)
diff --git a/my_callbacks.py b/my_callbacks.py
new file mode 100644
index 0000000..6640c9d
--- /dev/null
+++ b/my_callbacks.py
@@ -0,0 +1,21 @@
+from dash.dependencies import (
+    Input, 
+    Output
+)
+from dash import html
+
+
+def register_callbacks(app):
+    @app.callback(
+        Output('output-container-button', 'children'),
+        [Input('submit-btn', 'n_clicks')],
+        [Input('input-text', 'value')]
+    )
+    def update_output(n_clicks, input_value):
+        if n_clicks > 0:
+            return html.Div([
+                html.Label("You entered:"),
+                html.P(input_value)
+            ])
+        else:
+            return ''
diff --git a/my_layout.py b/my_layout.py
new file mode 100644
index 0000000..e0cf7c4
--- /dev/null
+++ b/my_layout.py
@@ -0,0 +1,13 @@
+from dash import html
+from dash import dcc
+
+
+layout = html.Div([
+    html.H1("Yeay, my app!"),
+    html.Div([
+        html.Label("Enter your text:"),
+        dcc.Input(id='input-text', type='text', value=''),
+        html.Button('Submit', id='submit-btn', n_clicks=0),
+    ]),
+    html.Div(id='output-container-button')
+])
diff --git a/requirements.txt b/requirements.txt
index e38d704..c157018 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,9 +1,14 @@
 jupyter-server-proxy==4.4.0
 
-streamlit
+dash
+dash-bootstrap-components
+flake8
+
 openai
 rapidfuzz
 nltk
+plotly
+scikit-learn
 
 pdfplumber
 python-dotenv
diff --git a/streamlit_app.py b/streamlit_app.py
deleted file mode 100644
index 4fd2a13..0000000
--- a/streamlit_app.py
+++ /dev/null
@@ -1,4 +0,0 @@
-import streamlit as st 
-
-
-st.title("This is my app!")
diff --git a/streamlit_proxy/my_app_proxy.py b/streamlit_proxy/my_app_proxy.py
deleted file mode 100644
index cfed48b..0000000
--- a/streamlit_proxy/my_app_proxy.py
+++ /dev/null
@@ -1,21 +0,0 @@
-def setup_my_app_proxy():
-    command = [
-        "/streamlit/app/run_my_app.sh",
-        "--browser.gatherUsageStats", "false",
-        "--browser.serverAddress", "0.0.0.0",
-        "--server.port", "{port}",
-        "--server.headless", "true",
-        "--server.enableCORS", "false",
-        "--server.enableXsrfProtection", "false",
-    ]
-
-    return {
-        "command": command,
-        "timeout": 20,
-        "new_browser_tab": False,
-        "launcher_entry": {
-            "enabled": True,
-            "title": "MyApp",
-            "icon_path": "/streamlit/app/streamlit-favicon.svg"
-        }
-    }
\ No newline at end of file
diff --git a/streamlit_proxy/streamlit_proxy.py b/streamlit_proxy/streamlit_proxy.py
deleted file mode 100644
index fd14608..0000000
--- a/streamlit_proxy/streamlit_proxy.py
+++ /dev/null
@@ -1,25 +0,0 @@
-import os
-
-
-def setup_streamlit_proxy():
-    command = [
-        "/streamlit/app/run_streamlit_app.sh",
-        "--browser.gatherUsageStats", "false",
-        "--browser.serverAddress", "0.0.0.0",
-        "--server.port", "{port}",
-        "--server.headless", "true",
-        "--server.enableCORS", "false",
-        "--server.enableXsrfProtection", "false",
-    ]
-
-
-    return {
-        "command": command,
-        "timeout": 20,
-        "new_browser_tab": False,
-        "launcher_entry": {
-            "enabled": True,
-            "title": "Streamlit App",
-            "icon_path": "/streamlit/app/streamlit-favicon.svg"
-        }
-    }
-- 
GitLab