update app params and update streamlit docs
authorClaromes <claromes@hey.com>
Tue, 25 Jun 2024 14:25:20 +0000 (11:25 -0300)
committerClaromes <claromes@hey.com>
Tue, 25 Jun 2024 14:25:20 +0000 (11:25 -0300)
app/app.py
docs/streamlit.rst

index b274267e68059e3e8e7b499ab6797e454a7e1a53..df41e67f016109958bfd875488d35f55b42ec687 100644 (file)
@@ -2,6 +2,7 @@ import base64
 from datetime import datetime, timedelta
 
 import streamlit as st
+import streamlit.components.v1 as components
 
 from waybacktweets.api.export import TweetsExporter
 from waybacktweets.api.parse import TweetsParser
@@ -56,11 +57,17 @@ if "count" not in st.session_state:
 if "archived_timestamp_filter" not in st.session_state:
     st.session_state.archived_timestamp_filter = (start_date, end_date)
 
-if "username_param" not in st.session_state:
-    st.session_state.username_param = ""
+if "username_value" not in st.session_state:
+    st.session_state.username_value = ""
 
-if "expanded_param" not in st.session_state:
-    st.session_state.expanded_param = False
+if "expanded_value" not in st.session_state:
+    st.session_state.expanded_value = False
+
+if "query" not in st.session_state:
+    st.session_state.query = False
+
+if "update_component" not in st.session_state:
+    st.session_state.update_component = 0
 
 if "username" not in st.query_params:
     st.query_params["username"] = ""
@@ -135,19 +142,34 @@ def tweets_exporter(parsed_tweets, username, field_options):
     return df, file_name
 
 
-# ------Query Params ------ #
+# ------ Custom JavaScript ------ #
+
+
+def scroll_page():
+    js = f"""
+    <script>
+        window.parent.document.querySelector('section.main').scrollTo(700, 700);
+        let update_component = {st.session_state.update_component} // Force component update to generate scroll
+    </script>
+    """  # noqa: E501
+
+    components.html(js, width=0, height=0)
+
+
+# ------ Query Param ------ #
 
 if st.query_params.username != "":
-    st.session_state.username_param = st.query_params.username
-    st.session_state.expanded_param = True
+    st.session_state.username_value = st.query_params.username
+    st.session_state.expanded_value = True
+    st.session_state.query = True
 
-if st.query_params.username == "":
-    st.query_params.clear()
+    st.session_state.update_component += 1
+    scroll_page()
 
 # ------ User Interface Settings ------ #
 
 st.info(
-    "🥳 [**Pre-release 1.0a5: Python module, CLI, and new Streamlit app**](https://github.com/claromes/waybacktweets/releases/tag/v1.0a5)"  # noqa: E501
+    "🥳 [**Pre-release 1.0x: Python module, CLI, and new Streamlit app**](https://github.com/claromes/waybacktweets/releases)"  # noqa: E501
 )
 
 st.image(TITLE, use_column_width="never")
@@ -172,12 +194,12 @@ st.divider()
 
 username = st.text_input(
     "Username *",
-    value=st.session_state.username_param,
+    value=st.session_state.username_value,
     key="username",
     placeholder="Without @",
 )
 
-with st.expander("Filtering", expanded=st.session_state.expanded_param):
+with st.expander("Filtering", expanded=st.session_state.expanded_value):
 
     st.session_state.archived_timestamp_filter = st.date_input(
         "Tweets saved between",
@@ -219,12 +241,16 @@ with st.expander("Filtering", expanded=st.session_state.expanded_param):
 
 query = st.button("Query", type="primary", use_container_width=True)
 
+if st.query_params.username == "":
+    st.query_params.clear()
+    st.session_state.query = query
+
 # ------ Results ------ #
 
 if username != st.session_state.current_username:
     st.session_state.current_username = username
 
-if query or st.session_state.count:
+if st.session_state.query or st.session_state.count:
     if unique:
         collapse = "urlkey"
         matchtype = "prefix"
index ff964758c6703fa941fe961e38c51e06150a9e37..1c5f25acbe96be8aefec0311f19180c0efe9e348 100644 (file)
@@ -18,6 +18,16 @@ Filters
 - Only unique Wayback Machine URLs: Filtering by the collapse option using the ``urlkey`` field and the URL Match Scope ``prefix``
 
 
+Username Query Parameter
+--------------------------
+
+An alternative way to access the application is by using the ``username`` query parameter. This allows for automatic configuration of the Username input and automatically searches. Additionally, when the ``username`` parameter is sent, the accordion with the filters will already be open.
+
+Example URL format:
+
+``https://waybacktweets.streamlit.app?username=<USERNAME>``
+
+
 Community Comments
 --------------------