Deploy sketch on streamlit

Hi all, I was trying to work with this library on the streamlit cloud, but the import doesn’t seem to succeed correctly.

import sketch
#some code

Can you help me figure out how to fix this error?

LINK : GitHub - approximatelabs/sketch: AI code-writing assistant that understands data content

Python v : 3.9
Libraries :

Successfully installed MarkupSafe-2.1.2 aiohttp-3.8.4 aiosignal-1.3.1 altair-4.2.2 asttokens-2.2.1 async-timeout-4.0.2 attrs-22.2.0 backcall-0.2.0 blinker-1.5 cachetools-5.3.0 certifi-2022.12.7 charset-normalizer-3.1.0 click-8.1.3 datasketch-1.5.9 datasketches-4.0.1 decorator-5.1.1 entrypoints-0.4 executing-1.2.0 frozenlist-1.3.3 gitdb-4.0.10 gitpython-3.1.31 idna-3.4 importlib-metadata-6.1.0 ipython-8.12.0 jedi-0.18.2 jinja2-3.1.2 jsonschema-4.17.3 lambdaprompt-0.4.2 markdown-it-py-2.2.0 matplotlib-inline-0.1.6 mdurl-0.1.2 multidict-6.0.4 nest-asyncio-1.5.6 numpy-1.24.2 packaging-23.0 pandas-1.5.3 parso-0.8.3 pexpect-4.8.0 pickleshare-0.7.5 pillow-9.4.0 prompt-toolkit-3.0.38 protobuf-3.20.3 ptyprocess-0.7.0 pure-eval-0.2.2 pyarrow-11.0.0 pydeck-0.8.0 pygments-2.14.0 pympler-1.0.1 pyrsistent-0.19.3 python-dateutil-2.8.2 python-dotenv-1.0.0 pytz-2023.3 pytz-deprecation-shim-0.1.0.post0 pyyaml-6.0 requests-2.28.2 rich-13.3.3 scipy-1.10.1 semver-2.13.0 six-1.16.0 sketch-0.3.5 smmap-5.0.0 stack-data-0.6.2 streamlit-1.20.0 streamlit-chat-media-0.0.3 tls-client-0.2 toml-0.10.2 toolz-0.12.0 tornado-6.2 traitlets-5.9.0 typing-extensions-4.5.0 tzdata-2023.3 tzlocal-4.3 urllib3-1.26.15 validators-0.20.0 watchdog-3.0.0 wcwidth-0.2.6 websocket-client-1.5.1 yarl-1.8.2 zipp-3.15.0

Hi @alessandro_ciciarell, welcome to the forum! :wave: :smile:

You’re encountering the error because the library you’re importing (sketch) imports another library (lambdaprompt) that uses async functions. But Streamlit runs in a separate thread that doesn’t have an event loop by default. To make it work, you’ll need to create an event loop and run the async functions inside it.

The lambdaprompt library runs some async code during the import process itself. So you can try creating an event loop before importing the library. Here’s an example:

import asyncio
from contextlib import contextmanager
import streamlit as st

# Create a context manager to run an event loop
def setup_event_loop():
    loop = asyncio.new_event_loop()
        yield loop

# Use the context manager to create an event loop
with setup_event_loop() as loop:
    import sketch

# Now you can use the 'sketch' library in your Streamlit app
st.write("The 'sketch' library has been successfully imported.")

I’m not 100% sure this is the right approach as I don’t have prior experience running async code in Streamlit. I’ll defer to the community to correct me.


Thank you so much for your quick response. I love Streamlit and its community.

Your solution works but I tried to change it to the minimum to avoid the error it gave me.

import streamlit as st
    page_title="🧠🤖🇮🇹 - Beta ChatBOT Intelligenza Artificiale Italia",
from streamlit_chat_media import message
import asyncio
loop = asyncio.new_event_loop()
import sketch

st.write("The 'sketch' library has been successfully imported.")

Now work well :smile:

Really thanks again