Gebruikershulpmiddelen

Site-hulpmiddelen


steamlit

Streamlit

Terug naar start

Python, LangChain, Streamlit, Embeddings, Vector DB ed.

Streamlit workspace

Streamlit Home
Streamlit on GitHub
Streamlit documentatie
Streamlit API referentie
Deploy Streamlit apps
Streamlit Extra's
Streamlit cheat-sheet
How to use Streamlit session states and callback functions | Make your apps remember things
Streamlit Elements You Should Know About

Publiceren:
:!: Streamlit workspace - Publiceren + testen Apps
YouTube video: How to Deploy Your App to Streamlit Community Cloud
Streamlit app in IFRAME tonen
- → IFRAME embeddings options

:!: How to Build Streamlit App & Deploy 🚀 (Python Project) - Tutorial for Beginner
:!: PLAY LIST YOUTUBE
:!: Deploy Your Streamlit App To Render (Free Heroku Alternative)

Video's:
Streamlit for ML #1 - Installation and API
Streamlit for ML #2 - Installation and APIs
Streamlit for ML #3 - Make Apps Fast with Caching!!!
Streamlit for ML #4 - Adding Bootstrap Components
Streamlit for ML #5.2 - MUI Card Component Build

❗ Chat met website:
Tutorial | Chat with any Website using Python and Langchain (LATEST VERSION)
GITHUB: Chat with a Website from URL - LangChain Chatbot with Streamlit GUI

YouTube: Chat with Multiple PDFs / LangChain App Tutorial in Python (Free LLMs and Embeddings)
YouTube: Create a ChatGPT clone using Streamlit and LangChain
YouTube: Langchain + Qdrant Cloud | Pinecone FREE Alternative (20GB) | Tutorial
YouTube: Langchain + ChatGPT + Pinecone: A Question Answering Streamlit App
YouTube: Geheugen: How to add memory to your LLM to remember previous conversation
Introducing st.connection for connecting to SQL database in 4 lines
st.experimental_connection

Serie vide's (volledige cursus)
How to Deploy Data Science Web App to Heroku | Streamlit

Changelog info m.b.t. de laatste versies


Markdown Cheat-sheet
Emoji's (kopiëren/plakken)


Installeren

pip install streamlit

:!: Bij problemen met gewoon installeren de onderstaande methode gebruiken

python -m pip install streamlit

Test of streamlit werkt:

streamlit hello

Een Streamlit App Starten:

streamlit run app.py

→ Upgrade streamlit (versie 1.30.0 nodig)

pip install --upgrade streamlit

@st.cache_resource

:!: st.cache_resource :!:
→ @st.cache_resource gebruiken voor database verbindingen en verbindingen met ML models

Zet boven function:

import streamlit as st

@st.cache_resource
def get_database_session(_sessionmaker, url):
    # Create a database connection object that points to the URL.
    return connection

s1 = get_database_session(create_sessionmaker(), DATA_URL_1)
# Actually executes the function, since this is the first time it was
# encountered.

s2 = get_database_session(create_sessionmaker(), DATA_URL_1)
# Does not execute the function. Instead, returns its previously computed
# value - even though the _sessionmaker parameter was different
# in both calls.
import streamlit as st

@st.cache_resource
def get_database_session(_sessionmaker, url):
    # Create a database connection object that points to the URL.
    return connection

get_database_session.clear()
# Clear all cached entries for this function.

:!: Als @st.cache_resource boven een Class functie staat moet i.p.v. self, _self worden gebruikt.
Dit benaderd dan de variabele in de Cash

@st.cache_resource
def get_llm(_self):
    llm = ChatOpenAI(model_name=_self.a3dmod.aimodel, temperature=_self.a3dmod.temperature)
    return llm

Session State

Youtube: Session State basics
Session State Tutorial

→ Controleer of een waarde al is session_state in opgenomen/aanwezig is:

# Initialization
if 'key' not in st.session_state:
    st.session_state['key'] = 'value'

# Session State also supports attribute based syntax
if 'key' not in st.session_state:
    st.session_state.key = 'value'          

→ Reads and updates:

st.write(st.session_state.key)

→ Update an item in Session State by assigning it a value::

st.session_state.key = 'value2'     # Attribute API
st.session_state['key'] = 'value2'  # Dictionary like API

→ Delete items:

# Delete a single key-value pair
del st.session_state[key]

# Delete all the items in Session state
for key in st.session_state.keys():
    del st.session_state[key]
</code

-> Session State and Widget State association:

<code>
st.text_input("Your name", key="name")

# This exists now:
st.session_state.name

Markdown

Emoji's

🚀 🎈 🤖 ❤️ ⚕️ ☯️ 🏥 🗨️ 💭 💡 ℹ️ ⚙️ 🔧 🚧 🏡 ⚠️ 🛟 ⛔ 🚫 🪜 🔒 🔓 🔑 ⛓️ ✨ 🪄 ⭐ ✨ 🎉 🎁 🛒 💰 💲 💫 📱 📧 📫 🚩 🏁 ❔ ❕ ♻️ ✔️ ➕ ❌ 🏷️ 📂 🗂️ 📆 ✏️ 🖋️ 📝 📄 🔔 ⏰ 🔍 🕵️ 👤 🧮 🧬 🌍 🧭 🏅 🏆 🤷‍♂️ 🕮 📖 📚 📗 📙 🔸 🔹 💎 👑


Preloader

Had beetje moeite om een goede preloader te vinden en gebruiken. Zelf een oplossing gevonden in de vorm van st.empty. Hiermee kun je een variabele vullen met tekst en als deze niet meer nodig is kun je deze verwijderen:

preloader = st.empty()   
preloader.text("🕵️ Een moment geduld a.u.b...") 

# En om dan de preloader verwijderen/te legen:
preloader.empty()

Systeem variabelen (geheimen ed.)

secrets.toml

Systeem variabelen - Beheer van geheimen

Wanneer u uw app lokaal ontwikkelt, voegt u een bestand toe met de naam secrets.toml in een map met .streamlitde hoofdmap van uw app-repository, en kopieert/plakt u uw geheimen in dat bestand. Verdere instructies zijn beschikbaar in de Streamlit- bibliotheekgeheimenbeheerdocumentatie

:!: Zorg ervoor dat u dit bestand aan uw bestand toevoegt, .gitignore zodat u uw geheimen niet prijsgeeft!
→ in online omgeving variabelen in de omgeving invoeren!

your-LOCAL-repository/
├── .streamlit/
│   ├── config.toml
│   └── secrets.toml # Make sure to gitignore this!
├── your_app.py
└── requirements.txt

config.toml

→ In het config.toml bestand kunt u verschillende configuratie-opties instellen, zoals:

[server]
port = 8501
headless = true

en b.v.:

[browser]
gatherUsageStats = false

[theme]
primaryColor = "#F63366"
backgroundColor = "#FFFFFF"
secondaryBackgroundColor = "#F0F2F6"

→ Aangepaste Thema's: Streamlit ondersteunt aangepaste thema's, zodat u de look en feel van uw app kunt afstemmen op uw merk of voorkeuren.


secrets.toml

→ Omgevingsvariabelen voor Streamlet worden in het “secrets.toml” bestand geplaatst in de “.streamlit” map:

OPENAI_API_KEY = "XXX"
PINECONE_API_KEY = "XXX"
PINECONE_ENVIRONMENT = "XXX"
PINECONE_INDEX_NAME = "XXX"

Deploy Streamlit app

Streamlit app publiceren in Streamlit Community Cloud

:!: Bij het aanmaken van de app niet vergeten de systeemvariabelen te zetten voor Streamlit

→ Volg video:
YouTube video: How to Deploy Your App to Streamlit Community Cloud

Streamlit workspace


Streamlit app tonen in iframe

<iframe
  src="https://30days.streamlit.app/?embed=true"
  height="800"
  style="width:100%;border:none;"
></iframe>

Data doorgeven op GET manier

Het is ook mogelijk GET data mee te geven in het iFrame zodat die data kan worden uitgelezen door/in de Streamlit app:

URL: http://192.168.1.76:8501/?naam=Jan
In streamlit app ophalen met:

url = st.experimental_get_query_params()
if 'naam' in url:
    naam = url['naam'][0]                  
    print(naam) 

→ Data doorgeven aan Streamlit app in Iframe:

<iframe
  src="https://30days.streamlit.app/?embed=true&naam=Jan"
  height="800"
  style="width:100%;border:none;"
></iframe>

Streamlit app op website tonen (Google)

steamlit.txt · Laatst gewijzigd: 2024/02/09 15:36 door a3dijke