Fix pagination/ prev and next button
authorClaromes <claromes@hey.com>
Fri, 19 May 2023 18:43:17 +0000 (15:43 -0300)
committerClaromes <claromes@hey.com>
Fri, 19 May 2023 18:43:17 +0000 (15:43 -0300)
README.md
app.py

index 8a7d7460965bde945c559387fe7c5ff7da03f5cb..89a587085eead7153c96453df52913a5d0c7f6c7 100644 (file)
--- a/README.md
+++ b/README.md
@@ -28,8 +28,9 @@ Streamlit will be served at http://localhost:8501
 
 - [ ] "web.archive.org took too long to respond."
 - [x] `only_deleted` checkbox selected for handles without deleted tweets
-    - [ ] Add message
-- [ ] Pagination: set session variable on first click
+    - [ ] Add message if handle has no deleted tweets
+- [x] Pagination: set session variable on first click
+- [ ] Pagination: scroll to top
 
 ## Roadmap
 
@@ -39,4 +40,4 @@ Streamlit will be served at http://localhost:8501
 - [ ] Feedbacks
 - [ ] Prevent duplicate URLs
 - [ ] Grid
-- [ ] Docs
\ No newline at end of file
+- [ ] About
\ No newline at end of file
diff --git a/app.py b/app.py
index 81d3a675f14d84f02278ab595aadb030d2e54026..38142c9d8ea63a5b0e189dcca043576ccf57ba7c 100644 (file)
--- a/app.py
+++ b/app.py
@@ -3,7 +3,7 @@ import datetime
 import streamlit as st
 import streamlit.components.v1 as components
 
-__version__ = '0.0.2'
+__version__ = '0.1.0'
 
 st.set_page_config(
     page_title='Wayback Tweets',
@@ -44,7 +44,7 @@ hide_streamlit_style = '''
 </style>
 '''
 
-#st.markdown(hide_streamlit_style, unsafe_allow_html=True)
+st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
 if 'current_index' not in st.session_state:
     st.session_state.current_index = 0
@@ -55,20 +55,17 @@ if 'current_query' not in st.session_state:
 if 'current_handle' not in st.session_state:
     st.session_state.current_handle = ''
 
-if 'disabled_next' not in st.session_state:
-    st.session_state.disabled_next = False
+if 'prev_disabled' not in st.session_state:
+    st.session_state.prev_disabled = False
 
-if 'disabled_prev' not in st.session_state:
-    st.session_state.disabled_prev = False
+if 'next_disabled' not in st.session_state:
+    st.session_state.next_disabled = False
 
-def scroll_into_view():
-    js = '''
-    <script>
-        window.parent.document.getElementById('wayback-tweets').scrollIntoView();
-    </script>
-    '''
+if 'next_button' not in st.session_state:
+    st.session_state.next_button = False
 
-    st.components.v1.html(js)
+if 'prev_button' not in st.session_state:
+    st.session_state.prev_button = False
 
 def embed(tweet):
     api = 'https://publish.twitter.com/oembed?url={}'.format(tweet)
@@ -99,7 +96,6 @@ def parse_links(links):
     tweet_links = []
     parsed_mimetype = []
 
-
     for link in links[1:]:
         url = 'https://web.archive.org/web/{}/{}'.format(link[1], link[2])
 
@@ -115,14 +111,12 @@ def attr(i):
     {}. **Wayback Machine:** [link]({}) | **MIME Type:** {} | **From:** {} | **Tweet:** [link]({})
     '''.format(i+1, link, mimetype[i], datetime.datetime.strptime(timestamp[i], "%Y%m%d%H%M%S"), tweet_links[i]))
 
-st.title('Wayback Tweets [![Fork me on GitHub](https://img.shields.io/badge/-Fork%20me%20on%20GitHub-ededed?logo=github&style=social)](https://github.com/claromes/waybacktweets)', anchor=False)
+st.title('Wayback Tweets [![GitHub release (latest by date)](https://img.shields.io/github/v/release/claromes/waybacktweets)](https://github.com/claromes/waybacktweets/releases)', anchor=False)
 st.write('Archived tweets on Wayback Machine')
 
 handle = st.text_input('username', placeholder='username', label_visibility='collapsed')
 query = st.button('Query', type='primary', use_container_width=True)
 
-
-
 if query or handle:
     if handle != st.session_state.current_handle:
         st.session_state.current_index = 0
@@ -147,7 +141,13 @@ if query or handle:
             st.session_state.current_query = query
 
             return_none_count = 0
-            tweets_per_page = 2
+            tweets_per_page = 50
+
+            def prev_page():
+                st.session_state.current_index -= tweets_per_page
+
+            def next_page():
+                st.session_state.current_index += tweets_per_page
 
             start_index = st.session_state.current_index
             end_index = min(len(parsed_links), start_index + tweets_per_page)
@@ -180,30 +180,20 @@ if query or handle:
 
                         progress.write('{}/{}-{} URLs have been captured'.format(return_none_count, start_index, end_index))
 
-                if start_index == 0:
-                    st.session_state.disabled_prev = True
+                if start_index <= 0:
+                    st.session_state.prev_disabled = True
                 else:
-                    st.session_state.disabled_prev = False
+                    st.session_state.prev_disabled = False
 
                 if i + 1 == len(parsed_links):
-                    st.session_state.disabled_next = True
+                    st.session_state.next_disabled = True
                 else:
-                    st.session_state.disabled_next = False
-
-            print(start_index, end_index)
+                    st.session_state.next_disabled = False
 
             prev, _ , next = st.columns([3, 4, 3])
 
-            if prev.button('Previous', disabled=st.session_state.disabled_prev, type='primary', use_container_width=True):
-                scroll_into_view()
-                st.session_state.current_index -= tweets_per_page
-
-            if next.button('Next', disabled=st.session_state.disabled_next, type='primary', use_container_width=True):
-                scroll_into_view()
-                st.session_state.current_index += tweets_per_page
-
-            # if st.session_state.current_index >= len(parsed_links):
-            #     st.session_state.current_index = 0
+            prev.button('Previous', disabled=st.session_state.prev_disabled, key='prev_button_key', on_click=prev_page, type='primary', use_container_width=True)
+            next.button('Next', disabled=st.session_state.next_disabled, key='next_button_key', on_click=next_page, type='primary', use_container_width=True)
 
         if not links:
             st.error('Unable to query the Wayback Machine API.')