question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add API to get complete url

See original GitHub issue

Add an API to get the url input in the browser with parameters and path

Problem

Streamlit does not provide an API for the above, right now.

Solution

st.get_url()

Which returns a string or some “url” object with the url of the app in the browser. This should include parameters.

We can just add a simple middleware or a simple hook into a Tornado handler and store this in a variable.

Additional context

https://discuss.streamlit.io/t/component-to-get-and-set-client-url/267/4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:28
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
banjthemancommented, May 19, 2020

Full steps

  1. Find tornado location
import tornado
print(tornado.__file__)

/home/banjtheman/miniconda3/lib/python3.7/site-packages/tornado/__init__.py

  1. Edit routing.py class _RoutingDelegate(httputil.HTTPMessageDelegate) headers_received function

File location based on first step… /home/banjtheman/miniconda3/lib/python3.7/site-packages/tornado/routing.py

class _RoutingDelegate(httputil.HTTPMessageDelegate):
    def __init__(self, router, server_conn, request_conn):
        self.server_conn = server_conn
        self.request_conn = request_conn
        self.delegate = None
        self.router = router  # type: Router

    def headers_received(self, start_line, headers):
        request = httputil.HTTPServerRequest(
            connection=self.request_conn,
            server_connection=self.server_conn,
            start_line=start_line, headers=headers)

        #added code start
        if request.query != '':
            # request.para = dict()
            self.request_conn.params.urlpara = dict()
            # self.server_conn.para = dict()
            psl = request.query.split('&')
            for ps in psl:
                pl = ps.split('=')
                # self.request_conn.para[pl[0]] = pl[1]
                # self.server_conn.para[pl[0]] = pl[1]
                # request.para[pl[0]] = pl[1]
                self.request_conn.params.urlpara[pl[0]]=pl[1]
        #added code end

        self.delegate = self.router.find_handler(request)
        if self.delegate is None:
            app_log.debug("Delegate for %s %s request not found",
                          start_line.method, start_line.path)
            self.delegate = _DefaultMessageDelegate(self.request_conn)

        return self.delegate.headers_received(start_line, headers)

    def data_received(self, chunk):
        return self.delegate.data_received(chunk)

    def finish(self):
        self.delegate.finish()

    def on_connection_close(self):
        self.delegate.on_connection_close()

  1. Call in streamlit

streamlit run test.py https://127.0.0.1:8501/?mypara1=99

import streamlit as st
from streamlit.server.Server import Server

sessions = Server.get_current()._session_info_by_id
session_id_key = list(sessions.keys())[0]
session = sessions[session_id_key]
urlPara = session.ws.request.connection.params.urlpara

st.write("URL PARAM:"+str(urlPara))
5reactions
benlindsaycommented, Jun 14, 2022

Streamlit cloud has additional app-specific paths added to the domain name, like https://share.streamlit.io/benlindsay/streamlit-sharing-tabs/main/app.py. I think a link like this: st.markdown("[link](/first-page)") points to something like “https://share.streamlit.io/first-page” rather than “https://share.streamlit.io/benlindsay/streamlit-sharing-tabs/main/app.py/first-page”. I try to get around that in that app I linked to with an environment variable set to the full URL (see here ) but that is very brittle, and get_url() would be far superior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to get the full url of the current api NOT of the original ...
Is this code to build the full url, which is going wrong: var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority); var uri = Url.
Read more >
4.2. API Basics: URL, Methods, Return Formats, Authentication
The API is accessed from the URL: http://www.domain.com/api/index.php. Specific methods are accessed by using the "method" query string parameter. Example: http ...
Read more >
What is an API URL Path? API URL Meaning [Explained]
An API URL Path is an address that allows you to access an API and its various features. Using one is as simple...
Read more >
URL API - MDN Web Docs - Mozilla
Chrome Edge URL Full support. Chrome32. more. Toggle history Full support. Edge12. Togg... URL() constructor Full support. Chrome19. Toggle history Full support. Edge12. footn... createObjectURL Full...
Read more >
URL Generation - The PHP Framework For Web Artisans
// Get the current URL without the query string... echo url()->current();. // ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found