SiddhantSadangi / st_supabase_connection

An easy-to-use Supabase connector for Streamlit that caches your API calls to make querying fast and cheap.
https://pypi.org/project/st-supabase-connection/
MIT License
91 stars 12 forks source link

URL string must be encoded in the client initialization #7

Closed Jun-Murakami closed 11 months ago

Jun-Murakami commented 11 months ago

Hi, thanks for providing this useful library. When initializing the client, reading the URL from the environment variable, it seems that if the string is not encoded before passing it, it is interpreted internally as binary data and an error occurs. If this is not the intended behavior, it may be better to correct it.

Codes that cause errors

supabase_url = os.environ.get("SUPABSE_URL").encode()
supabase_key = os.environ.get("SUPABSE_KEY").encode()
supabase = st.experimental_connection(
    name="mma_supabase",
    type=SupabaseConnection,
    ttl=None,
    url=supabase_url
    key=supabase_key
)

Codes that work

supabase_url = os.environ.get("SUPABSE_URL").encode()
supabase_key = os.environ.get("SUPABSE_KEY").encode()
supabase = st.experimental_connection(
    name="mma_supabase",
    type=SupabaseConnection,
    ttl=None,
    url=supabase_url.decode("utf-8") # add
    key=supabase_key.decode("utf-8")
)

Trace

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 541, in _run_script exec(code, module.dict) File "C:\Users\conta\Dropbox_Presets\Python\MagesMusicAnalyzer\src\main.py", line 354, in supabase = st.experimental_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\connection_factory.py", line 285, in connection_factory return _create_connection( ^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\metrics_util.py", line 367, in wrapped_func result = non_optional_func(args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\connection_factory.py", line 91, in _create_connection return __create_connection(name, connection_class, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 211, in wrapper return cached_func(args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 240, in call return self._get_or_create_cached_value(args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 266, in _get_or_create_cached_value return self._handle_cache_miss(cache, value_key, func_args, func_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 320, in _handle_cache_miss computed_value = self._info.func(func_args, func_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\runtime\connection_factory.py", line 84, in __create_connection return connection_class(connection_name=name, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\streamlit\connections\base_connection.py", line 71, in init self._raw_instance: Optional[RawConnectionT] = self._connect(**kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\st_supabase_connection__init.py", line 63, in _connect self.client = create_client(url, key) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\supabase\client.py", line 284, in create_client return Client(supabase_url=supabase_url, supabase_key=supabase_key, options=options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\site-packages\supabase\client.py", line 52, in init if not re.match(r"^(https?)://.+", supabase_url): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\conta\anaconda3\envs\python311\Lib\re\init__.py", line 166, in match return _compile(pattern, flags).match(string) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: cannot use a string pattern on a bytes-like object

Jun-Murakami commented 11 months ago

Sorry, I overlooked the .encode() method added to my environment variable retrieval code...

SiddhantSadangi commented 11 months ago

Hey @Jun-Murakami , thanks for using this package, and glad you were able to get the issue sorted 🎉

Just a note, if you have your SUPABSE_URL and SUPABSE_KEY environment variables set, you don't need to explicitly pass them to st.experimental_connection(). They will be picked up automatically :)

Jun-Murakami commented 11 months ago

@SiddhantSadangi I see. That is very convenient!