Skip to main content

Modal text input component for Streamlit

Project description

streamlit-modal-input

Streamlit component providing a 'modal' text input widget (actually pausing the script until a string is entered and returned). Implemented using the firebase-user package to establish direct communication between the python backend and the React frontend.

It is meant first and foremost as a target widget for stdin redirection in case you wish to embed a live python interpreter in your streamlit app.

Routing the interpreter's output to display widgets is fairly easy, but due to how Streamlit works, redirecting stdin to a widget in the app and achieving the expected blocking behavior was not that simple...

Installation instructions

pip install streamlit-modal-input

Usage instructions

This widget is meant to be used with the firebase-user package as follows.

You first declare a firebase user client with your firebase app json configuration file and authenticate your user:

from firebase_user import FirebaseClient

if 'firebase_client' not in st.session_state:
    with open("app_config.json",'r') as f:
        config=json.load(f)
    st.session_state.firebase_client=FirebaseClient(config)
    st.session_state.firebase_client.auth.sign_in(user_email,user_password)

Then you can call the modal input widget, passing it the authenticated client.

text=modal_input(
    prompt,
    firebase_client=st.session_state.firebase_client,
    key="my_modal_input"
    )

This will start a firestore listener thread watching for changes in a specific firestore document (collection='streamlit_modal_input',document=user.email).

Obviously your user should have the suitable permissions to read and write to this document in your firestore security rules.

A custom input widget is then rendered on the frontend, whose output is routed to this firestore document (via the firebase REST API).

The main python script will wait for the listener to receive the string before resuming execution, achieving a similar behavior as regular python input function.

Note that this is a one-shot input: After the initial string has been received, the result will be memorized in session_state and subsequent reruns will show a disabled widget returning the same string again.

Example

import streamlit as st
from streamlit_modal_input import modal_input
from firebase_user import FirebaseClient

if 'firebase_client' not in st.session_state:
    with open("app_config.json",'r') as f:
        config=json.load(f)
    st.session_state.firebase_client=FirebaseClient(config)
    st.session_state.firebase_client.auth.sign_in(user_email,user_password)


text=modal_input(
    prompt,
    firebase_client=st.session_state.firebase_client,
    key="my_modal_input"
    )

#The rest of the script will execute only when the input string has been received from the frontend

st.write(text)

#Now click the button below to trigger a rerun and see what happens : the widget gets disabled, but still returns the same string you entered at first pass.

st.button("Rerun")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_modal_input-0.0.12.tar.gz (523.8 kB view details)

Uploaded Source

Built Distribution

streamlit_modal_input-0.0.12-py3-none-any.whl (3.0 MB view details)

Uploaded Python 3

File details

Details for the file streamlit_modal_input-0.0.12.tar.gz.

File metadata

File hashes

Hashes for streamlit_modal_input-0.0.12.tar.gz
Algorithm Hash digest
SHA256 7b25819b7173f40daf2e662b5c5c50e344fc914fa9faab3e04a254aa16dd86cf
MD5 21b04cf93dc31389d6fd00562f621210
BLAKE2b-256 caf1937be5fee63c9fed6f0366e8773095a891a2b9ca043f5cc14a9e6eaed214

See more details on using hashes here.

File details

Details for the file streamlit_modal_input-0.0.12-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_modal_input-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 17e5cedeb03b6d31186ef34ac7f3f1bcfbc387fea35aa192fffd57ff192062b0
MD5 c1b4acd9e16897aa05b3b4bda32663aa
BLAKE2b-256 1a9d7eb771959b77a450ea6f6abbc5c450393d4208be4513d140e97785cba4b8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page