AEUPH commited on
Commit
831d153
·
verified ·
1 Parent(s): 80d39a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -43
app.py CHANGED
@@ -1,47 +1,31 @@
1
  import streamlit as st
2
- import requests
3
- from urllib.parse import urlencode, quote_plus, urlparse
4
 
5
- def forward_parameters(target_url, extra_params):
6
- """
7
- Forward parameters to the target URL and return the response.
 
8
 
9
- :param target_url: The URL to which the parameters should be forwarded.
10
- :param extra_params: A dictionary of additional parameters to forward.
11
- :return: The response from the target URL.
12
- """
13
- # Validate the target URL
14
- parsed_url = urlparse(target_url)
15
- if parsed_url.scheme not in ['http', 'https']:
16
- return f"Invalid URL: '{target_url}'. URLs must start with http:// or https://"
17
-
18
- # Construct the full URL with extra parameters
19
- encoded_params = urlencode(extra_params, quote_via=quote_plus)
20
- full_url = f"{target_url}?{encoded_params}" if encoded_params else target_url
21
- try:
22
- response = requests.get(full_url) # Using GET request for simplicity
23
- return response.text
24
- except requests.exceptions.RequestException as e:
25
- # Return error message in case of request failure
26
- return f"An error occurred while forwarding parameters: {str(e)}"
27
-
28
- # Streamlit interface
29
- st.title('HTTPS Streamlit Parameters Forwarder')
30
-
31
- # Accessing the query parameters
32
- params = st.query_params
33
-
34
- target_url = params.get('param1', [None])[0]
35
-
36
- if target_url:
37
- # Remove 'param1' from the parameters to be forwarded
38
- forwarded_params = {k: v for k, v in params.items() if k != 'param1'}
39
- response = forward_parameters(target_url, forwarded_params)
40
- st.text_area("Response from Target URL", value=response, height=300)
41
- else:
42
- st.error("Please provide a target URL using `?param1=URL` in the query string.")
43
- st.markdown("""
44
- **Example usage:**
45
 
46
- Append `?param1=http://example.com&param2=value` to the query string in the address bar. This will forward `param2=value` to `http://example.com`.
47
- """)
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from streamlit.components.v1 import html
 
3
 
4
+ # Function to extract query parameters
5
+ def get_query_params():
6
+ # Streamlit provides st.experimental_get_query_params() to access query parameters
7
+ return st.experimental_get_query_params()
8
 
9
+ # Main function where we handle the redirect
10
+ def main():
11
+ # Get query parameters
12
+ query_params = get_query_params()
13
+
14
+ # Check if 'param1' exists in the query parameters
15
+ if 'param1' in query_params:
16
+ # Extract the first value of 'param1'
17
+ redirect_url = query_params['param1'][0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Use JavaScript to redirect to the URL provided in 'param1'
20
+ js = f"""
21
+ <script>
22
+ window.location.href = "{redirect_url}";
23
+ </script>
24
+ """
25
+ html(js) # Use Streamlit's HTML component to run the JavaScript
26
+ else:
27
+ # If 'param1' is not present, display a message
28
+ st.write("No redirect URL provided in query parameters.")
29
+
30
+ if __name__ == "__main__":
31
+ main()