File size: 32,680 Bytes
5c2c2a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 |
STREAMLIT DOCS
EVERNOTE :
https://www.evernote.com/shard/s313/nl/41973486/e7b40a50-9f4c-d816-2dc1-ae078af37fc8/
25-03-2024
—————
Build a basic LLM chat app - Streamlit Docs
https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps
Build a basic LLM chat app
Introduction
The advent of large language models like GPT has revolutionized the ease of developing chat-based applications. Streamlit offers several Chat elements, enabling you to build Graphical User Interfaces (GUIs) for conversational agents or chatbots. Leveraging session state along with these elements allows you to construct anything from a basic chatbot to a more advanced, ChatGPT-like experience using purely Python code.
In this tutorial, we'll start by walking through Streamlit's chat elements, st.chat_message and st.chat_input. Then we'll proceed to construct three distinct applications, each showcasing an increasing level of complexity and functionality:
1. First, we'll Build a bot that mirrors your input to get a feel for the chat elements and how they work. We'll also introduce session state and how it can be used to store the chat history. This section will serve as a foundation for the rest of the tutorial.
2. Next, you'll learn how to Build a simple chatbot GUI with streaming.
3. Finally, we'll Build a ChatGPT-like app that leverages session state to remember conversational context, all within less than 50 lines of code.
Here's a sneak peek of the LLM-powered chatbot GUI with streaming we'll build in this tutorial:
Play around with the above demo to get a feel for what we'll build in this tutorial. A few things to note:
* There's a chat input at the bottom of the screen that's always visible. It contains some placeholder text. You can type in a message and press Enter or click the run button to send it.
* When you enter a message, it appears as a chat message in the container above. The container is scrollable, so you can scroll up to see previous messages. A default avatar is displayed to your messages' left.
* The assistant's responses are streamed to the frontend and are displayed with a different default avatar.
Before we start building, let's take a closer look at the chat elements we'll use.
Chat elements
Streamlit offers several commands to help you build conversational apps. These chat elements are designed to be used in conjunction with each other, but you can also use them separately.
st.chat_message lets you insert a chat message container into the app so you can display messages from the user or the app. Chat containers can contain other Streamlit elements, including charts, tables, text, and more. st.chat_input lets you display a chat input widget so the user can type in a message.
For an overview of the API, check out this video tutorial by Chanin Nantasenamat (@dataprofessor), a Senior Developer Advocate at Streamlit.
Streamlit
13,7K abonnees
Introducing Streamlit Chat Elements
st.chat_message
st.chat_message lets you insert a multi-element chat message container into your app. The returned container can contain any Streamlit element, including charts, tables, text, and more. To add elements to the returned container, you can use with notation.
st.chat_message's first parameter is the name of the message author, which can be either "user" or "assistant" to enable preset styling and avatars, like in the demo above. You can also pass in a custom string to use as the author name. Currently, the name is not shown in the UI but is only set as an accessibility label. For accessibility reasons, you should not use an empty string.
Here's an minimal example of how to use st.chat_message to display a welcome message:
import streamlit as st with st.chat_message("user"): st.write("Hello 👋")
Notice the message is displayed with a default avatar and styling since we passed in "user" as the author name. You can also pass in "assistant" as the author name to use a different default avatar and styling, or pass in a custom name and avatar. See the API reference for more details.
import streamlit as st import numpy as np with st.chat_message("assistant"): st.write("Hello human") st.bar_chart(np.random.randn(30, 3))
While we've used the preferred with notation in the above examples, you can also just call methods directly in the returned objects. The below example is equivalent to the one above:
import streamlit as st
import numpy as np
message = st.chat_message("assistant") message.write("Hello human") message.bar_chart(np.random.randn(30, 3))
So far, we've displayed predefined messages. But what if we want to display messages based on user input?
st.chat_input
st.chat_input lets you display a chat input widget so the user can type in a message. The returned value is the user's input, which is None if the user hasn't sent a message yet. You can also pass in a default prompt to display in the input widget. Here's an example of how to use st.chat_input to display a chat input widget and show the user's input:
import streamlit as st
prompt = st.chat_input("Say something")
if prompt:
st.write(f"User has sent the following prompt: {prompt}")
Pretty straightforward, right? Now let's combine st.chat_message and st.chat_input to build a bot the mirrors or echoes your input.
Build a bot that mirrors your input
In this section, we'll build a bot that mirrors or echoes your input. More specifically, the bot will respond to your input with the same message. We'll use st.chat_message to display the user's input and st.chat_input to accept user input. We'll also use session state to store the chat history so we can display it in the chat message container.
First, let's think about the different components we'll need to build our bot:
* Two chat message containers to display messages from the user and the bot, respectively.
* A chat input widget so the user can type in a message.
* A way to store the chat history so we can display it in the chat message containers. We can use a list to store the messages, and append to it every time the user or bot sends a message. Each entry in the list will be a dictionary with the following keys: role (the author of the message), and content (the message content).
import streamlit as st st.title("Echo Bot")
if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"])
In the above snippet, we've added a title to our app and a for loop to iterate through the chat history and display each message in the chat message container (with the author role and message content). We've also added a check to see if the messages key is in st.session_state. If it's not, we initialize it to an empty list. This is because we'll be adding messages to the list later on, and we don't want to overwrite the list every time the app reruns.
Now let's accept user input with st.chat_input, display the user's message in the chat message container, and add it to the chat history.
if prompt := st.chat_input("What is up?"):
with st.chat_message("user"): st.markdown(prompt) st.session_state.messages.append({"role": "user", "content": prompt})
We used the := operator to assign the user's input to the prompt variable and checked if it's not None in the same line. If the user has sent a message, we display the message in the chat message container and append it to the chat history.
All that's left to do is add the chatbot's responses within the if block. We'll use the same logic as before to display the bot's response (which is just the user's prompt) in the chat message container and add it to the history.
response = f"Echo: {prompt}" with st.chat_message("assistant"): st.markdown(response) st.session_state.messages.append({"role": "assistant", "content": response})
Putting it all together, here's the full code for our simple chatbot GUI and the result:
View full codeexpand_more
import streamlit as st
st.title("Echo Bot")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
response = f"Echo: {prompt}"
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
While the above example is very simple, it's a good starting point for building more complex conversational apps. Notice how the bot responds instantly to your input. In the next section, we'll add a delay to simulate the bot "thinking" before responding.
Build a simple chatbot GUI with streaming
In this section, we'll build a simple chatbot GUI that responds to user input with a random message from a list of pre-determind responses. In the next section, we'll convert this simple toy example into a ChatGPT-like experience using OpenAI.
Just like previously, we still require the same components to build our chatbot. Two chat message containers to display messages from the user and the bot, respectively. A chat input widget so the user can type in a message. And a way to store the chat history so we can display it in the chat message containers.
Let's just copy the code from the previous section and add a few tweaks to it.
import streamlit as st
import random
import time
st.title("Simple chat")
if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if prompt := st.chat_input("What is up?"): with st.chat_message("user"): st.markdown(prompt) st.session_state.messages.append({"role": "user", "content": prompt})
The only difference so far is we've changed the title of our app and added imports for random and time. We'll use random to randomly select a response from a list of responses and time to add a delay to simulate the chatbot "thinking" before responding.
All that's left to do is add the chatbot's responses within the if block. We'll use a list of responses and randomly select one to display. We'll also add a delay to simulate the chatbot "thinking" before responding (or stream its response). Let's make a helper function for this and insert it at the top of our app.
def response_generator():
response = random.choice( [ "Hello there! How can I assist you today?", "Hi, human! Is there anything I can help you with?", "Do you need help?", ] ) for word in response.split(): yield word + " " time.sleep(0.05)
Back to writing the response in our chat interface, we'll use st.write_stream to write out the streamed response with a typewriter effect.
with st.chat_message("assistant"):
response = st.write_stream(response_generator()) st.session_state.messages.append({"role": "assistant", "content": response})
Above, we've added a placeholder to display the chatbot's response. We've also added a for loop to iterate through the response and display it one word at a time. We've added a delay of 0.05 seconds between each word to simulate the chatbot "thinking" before responding. Finally, we append the chatbot's response to the chat history. As you've probably guessed, this is a naive implementation of streaming. We'll see how to implement streaming with OpenAI in the next section.
Putting it all together, here's the full code for our simple chatbot GUI and the result:
View full codeexpand_more
import streamlit as st
import random
import time
# Streamed response emulator
def response_generator():
response = random.choice(
[
"Hello there! How can I assist you today?",
"Hi, human! Is there anything I can help you with?",
"Do you need help?",
]
)
for word in response.split():
yield word + " "
time.sleep(0.05)
st.title("Simple chat")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input
if prompt := st.chat_input("What is up?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
response = st.write_stream(response_generator())
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
Play around with the above demo to get a feel for what we've built. It's a very simple chatbot GUI, but it has all the components of a more sophisticated chatbot. In the next section, we'll see how to build a ChatGPT-like app using OpenAI.
Build a ChatGPT-like app
Now that you've understood the basics of Streamlit's chat elements, let's make a few tweaks to it to build our own ChatGPT-like app. You'll need to install the OpenAI Python library and get an API key to follow along.
Install dependencies
First let's install the dependencies we'll need for this section:
pip install openai streamlit
Add OpenAI API key to Streamlit secrets
Next, let's add our OpenAI API key to Streamlit secrets. We do this by creating .streamlit/secrets.toml file in our project directory and adding the following lines to it:
OPENAI_API_KEY = "YOUR_API_KEY"
Write the app
Now let's write the app. We'll use the same code as before, but we'll replace the list of responses with a call to the OpenAI API. We'll also add a few more tweaks to make the app more ChatGPT-like.
import streamlit as st from openai import OpenAI st.title("ChatGPT-like clone") client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) if "openai_model" not in st.session_state: st.session_state["openai_model"] = "gpt-3.5-turbo" if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if prompt := st.chat_input("What is up?"): st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt)
All that's changed is that we've added a default model to st.session_state and set our OpenAI API key from Streamlit secrets. Here's where it gets interesting. We can replace our emulated stream with the model's responses from OpenAI:
with st.chat_message("assistant"): stream = client.chat.completions.create( model=st.session_state["openai_model"], messages=[ {"role": m["role"], "content": m["content"]} for m in st.session_state.messages ], stream=True, ) response = st.write_stream(stream) st.session_state.messages.append({"role": "assistant", "content": response})
Above, we've replaced the list of responses with a call to OpenAI().chat.completions.create. We've set stream=True to stream the responses to the frontend. In the API call, we pass the model name we hardcoded in session state and pass the chat history as a list of messages. We also pass the role and content of each message in the chat history. Finally, OpenAI returns a stream of responses (split into chunks of tokens), which we iterate through and display each chunk.
Putting it all together, here's the full code for our ChatGPT-like app and the result:
View full codeexpand_more
from openai import OpenAI
import streamlit as st
st.title("ChatGPT-like clone")
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
stream = client.chat.completions.create(
model=st.session_state["openai_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
)
response = st.write_stream(stream)
st.session_state.messages.append({"role": "assistant", "content": response})
Congratulations! You've built your own ChatGPT-like app in less than 50 lines of code.
We're very excited to see what you'll build with Streamlit's chat elements. Experiment with different models and tweak the code to build your own conversational apps. If you build something cool, let us know on the Forum or check out some other Generative AI apps for inspiration. 🎈
forum
Still have questions?
Our forums are full of helpful information and Streamlit experts.
…
ZIE OOK:
AI-Yash/st-chat: Streamlit Component, for a Chatbot UI
https://github.com/AI-Yash/st-chat
…
Build an LLM app using LangChain - Streamlit Docs
https://docs.streamlit.io/knowledge-base/tutorials/llm-quickstart
Build an LLM app using LangChain
OpenAI, LangChain, and Streamlit in 18 lines of code
In this tutorial, you will build a Streamlit LLM app that can generate text from a user-provided prompt. This Python app will use the LangChain framework and Streamlit. Optionally, you can deploy your app to Streamlit Community Cloud when you're done.
This tutorial is adapted from a blog post by Chanin Nantesanamat: LangChain tutorial #1: Build an LLM-powered app in 18 lines of code.
Objectives
1. Get an OpenAI key from the end user.
2. Validate the user's OpenAI key.
3. Get a text prompt from the user.
4. Authenticate OpenAI with the user's key.
5. Send the user's prompt to OpenAI's API.
6. Get a response and display it.
Bonus: Deploy the app on Streamlit Community Cloud!
Prerequisites
* Python 3.8+
* Streamlit
* LangChain
* OpenAI API key
Setup coding environment
In your IDE (integrated coding environment), open the terminal and install the following three Python libraries:
pip install streamlit openai langchain
Create a requirements.txt file located in the root of your working directory and save these dependencies. This is necessary for deploying the app to the Streamlit Community Cloud later.
streamlit openai langchain
Building the app
The app is only 18 lines of code:
import streamlit as st from langchain.llms import OpenAI st.title('🦜🔗 Quickstart App') openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password') def generate_response(input_text): llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key) st.info(llm(input_text)) with st.form('my_form'): text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?') submitted = st.form_submit_button('Submit') if not openai_api_key.startswith('sk-'): st.warning('Please enter your OpenAI API key!', icon='⚠') if submitted and openai_api_key.startswith('sk-'): generate_response(text)
To start, create a new Python file and save it as streamlit_app.py in the root of your working directory.
1. Import the necessary Python libraries.
import streamlit as st from langchain.llms import OpenAI
2. Create the app's title using st.title.
st.title('🦜🔗 Quickstart App')
3. Add a text input box for the user to enter their OpenAI API key.
openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password')
4. Define a function to authenticate to OpenAI API with the user's key, send a prompt, and get an AI-generated response. This function accepts the user's prompt as an argument and displays the AI-generated response in a blue box using st.info.
def generate_response(input_text): llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key) st.info(llm(input_text))
5. Finally, use st.form() to create a text box (st.text_area()) for user input. When the user clicks Submit, the generate-response() function is called with the user's input as an argument.
with st.form('my_form'): text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?') submitted = st.form_submit_button('Submit') if not openai_api_key.startswith('sk-'): st.warning('Please enter your OpenAI API key!', icon='⚠') if submitted and openai_api_key.startswith('sk-'): generate_response(text)
6. Remember to save your file!
7. Return to your computer's terminal to run the app.
streamlit run streamlit_app.py
Deploying the app
To deploy the app to the Streamlit Cloud, follow these steps:
1. Create a GitHub repository for the app. Your repository should contain two files:
your-repository/ ├── streamlit_app.py └── requirements.txt
2. Go to Streamlit Community Cloud, click the New app button from your workspace, then specify the repository, branch, and main file path. Optionally, you can customize your app's URL by choosing a custom subdomain.
3. Click the Deploy! button.
Your app will now be deployed to Streamlit Community Cloud and can be accessed from around the world! 🌎
Conclusion
Congratulations on building an LLM-powered Streamlit app in 18 lines of code! 🥳 You can use this app to generate text from any prompt that you provide. The app is limited by the capabilities of the OpenAI LLM, but it can still be used to generate some creative and interesting text.
We hope you found this tutorial helpful! Check out more examples to see the power of Streamlit and LLM. 💖
Happy Streamlit-ing! 🎈
forum
Still have questions?
Our forums are full of helpful information and Streamlit experts.
—————
Cheat sheet - Streamlit Docs
https://docs.streamlit.io/library/cheatsheet
Cheat Sheet
This is a summary of the docs, as of Streamlit v1.31.0.
Install & Import
pip install streamlit
streamlit run first_app.py
# Import convention
>>> import streamlit as st
Pre-release features
pip uninstall streamlit
pip install streamlit-nightly --upgrade
Learn more about experimental features
Command line
streamlit --help
streamlit run your_script.py
streamlit hello
streamlit config show
streamlit cache clear
streamlit docs
streamlit --version
Magic commands
# Magic commands implicitly
# call st.write().
"_This_ is some **Markdown***"
my_variable
"dataframe:", my_data_frame
Display text
st.write("Most objects") # df, err, func, keras!
st.write(["st", "is <", 3]) # see *
st.write_stream(my_generator)
st.write_stream(my_llm_stream)
st.text("Fixed width text")
st.markdown("_Markdown_") # see *
st.latex(r""" e^{i\pi} + 1 = 0 """)
st.title("My title")
st.header("My header")
st.subheader("My sub")
st.code("for i in range(8): foo()")
* optional kwarg unsafe_allow_html = True
Display data
st.dataframe(my_dataframe)
st.table(data.iloc[0:10])
st.json({"foo":"bar","fu":"ba"})
st.metric("My metric", 42, 2)
Display media
st.image("./header.png")
st.audio(data)
st.video(data)
st.video(data, subtitles="./subs.vtt")
Display charts
st.area_chart(df)
st.bar_chart(df)
st.line_chart(df)
st.map(df)
st.scatter_chart(df)
st.altair_chart(chart)
st.bokeh_chart(fig)
st.graphviz_chart(fig)
st.plotly_chart(fig)
st.pydeck_chart(chart)
st.pyplot(fig)
st.vega_lite_chart(df)
Add widgets to sidebar
# Just add it after st.sidebar:
>>> a = st.sidebar.radio("Select one:", [1, 2])
# Or use "with" notation:
>>> with st.sidebar:
>>> st.radio("Select one:", [1, 2])
Columns
# Two equal columns:
>>> col1, col2 = st.columns(2)
>>> col1.write("This is column 1")
>>> col2.write("This is column 2")
# Three different columns:
>>> col1, col2, col3 = st.columns([3, 1, 1])
# col1 is larger.
# You can also use "with" notation:
>>> with col1:
>>> st.radio("Select one:", [1, 2])
Tabs
# Insert containers separated into tabs:
>>> tab1, tab2 = st.tabs(["Tab 1", "Tab2"])
>>> tab1.write("this is tab 1")
>>> tab2.write("this is tab 2")
# You can also use "with" notation:
>>> with tab1:
>>> st.radio("Select one:", [1, 2])
Expandable containers
>>> expand = st.expander("My label")
>>> expand.write("Inside the expander.")
>>> pop = st.popover("Button label")
>>> pop.checkbox("Show all")
# You can also use "with" notation:
>>> with expand:
>>> st.radio("Select one:", [1, 2])
Control flow
# Stop execution immediately:
st.stop()
# Rerun script immediately:
st.rerun()
# Navigate to another page:
st.switch_page("pages/my_page.py")
# Group multiple widgets:
>>> with st.form(key="my_form"):
>>> username = st.text_input("Username")
>>> password = st.text_input("Password")
>>> st.form_submit_button("Login")
Display interactive widgets
st.button("Click me")
st.download_button("Download file", data)
st.link_button("Go to gallery", url)
st.page_link("app.py", label="Home")
st.data_editor("Edit data", data)
st.checkbox("I agree")
st.toggle("Enable")
st.radio("Pick one", ["cats", "dogs"])
st.selectbox("Pick one", ["cats", "dogs"])
st.multiselect("Buy", ["milk", "apples", "potatoes"])
st.slider("Pick a number", 0, 100)
st.select_slider("Pick a size", ["S", "M", "L"])
st.text_input("First name")
st.number_input("Pick a number", 0, 10)
st.text_area("Text to translate")
st.date_input("Your birthday")
st.time_input("Meeting time")
st.file_uploader("Upload a CSV")
st.camera_input("Take a picture")
st.color_picker("Pick a color")
# Use widgets' returned values in variables:
>>> for i in range(int(st.number_input("Num:"))):
>>> foo()
>>> if st.sidebar.selectbox("I:",["f"]) == "f":
>>> b()
>>> my_slider_val = st.slider("Quinn Mallory", 1, 88)
>>> st.write(slider_val)
# Disable widgets to remove interactivity:
>>> st.slider("Pick a number", 0, 100, disabled=True)
Build chat-based apps
# Insert a chat message container.
>>> with st.chat_message("user"):
>>> st.write("Hello 👋")
>>> st.line_chart(np.random.randn(30, 3))
# Display a chat input widget at the bottom of the app.
>>> st.chat_input("Say something")
# Display a chat input widget inline.
>>> with st.container():
>>> st.chat_input("Say something")
Learn how to Build a basic LLM chat app
Mutate data
# Add rows to a dataframe after
# showing it.
>>> element = st.dataframe(df1)
>>> element.add_rows(df2)
# Add rows to a chart after
# showing it.
>>> element = st.line_chart(df1)
>>> element.add_rows(df2)
Display code
>>> with st.echo():
>>> st.write("Code will be executed and printed")
Placeholders, help, and options
# Replace any single element.
>>> element = st.empty()
>>> element.line_chart(...)
>>> element.text_input(...) # Replaces previous.
# Insert out of order.
>>> elements = st.container()
>>> elements.line_chart(...)
>>> st.write("Hello")
>>> elements.text_input(...) # Appears above "Hello".
st.help(pandas.DataFrame)
st.get_option(key)
st.set_option(key, value)
st.set_page_config(layout="wide")
st.query_params[key]
st.query_params.get_all(key)
st.query_params.clear()
Connect to data sources
st.connection("pets_db", type="sql")
conn = st.connection("sql")
conn = st.connection("snowflake")
>>> class MyConnection(BaseConnection[myconn.MyConnection]):
>>> def _connect(self, **kwargs) -> MyConnection:
>>> return myconn.connect(**self._secrets, **kwargs)
>>> def query(self, query):
>>> return self._instance.query(query)
Optimize performance
Cache data objects
# E.g. Dataframe computation, storing downloaded data, etc.
>>> @st.cache_data
... def foo(bar):
... # Do something expensive and return data
... return data
# Executes foo
>>> d1 = foo(ref1)
# Does not execute foo
# Returns cached item by value, d1 == d2
>>> d2 = foo(ref1)
# Different arg, so function foo executes
>>> d3 = foo(ref2)
# Clear all cached entries for this function
>>> foo.clear()
# Clear values from *all* in-memory or on-disk cached functions
>>> st.cache_data.clear()
Cache global resources
# E.g. TensorFlow session, database connection, etc.
>>> @st.cache_resource
... def foo(bar):
... # Create and return a non-data object
... return session
# Executes foo
>>> s1 = foo(ref1)
# Does not execute foo
# Returns cached item by reference, s1 == s2
>>> s2 = foo(ref1)
# Different arg, so function foo executes
>>> s3 = foo(ref2)
# Clear all cached entries for this function
>>> foo.clear()
# Clear all global resources from cache
>>> st.cache_resource.clear()
Deprecated caching
>>> @st.cache
... def foo(bar):
... # Do something expensive in here...
... return data
>>> # Executes foo
>>> d1 = foo(ref1)
>>> # Does not execute foo
>>> # Returns cached item by reference, d1 == d2
>>> d2 = foo(ref1)
>>> # Different arg, so function foo executes
>>> d3 = foo(ref2)
Display progress and status
# Show a spinner during a process
>>> with st.spinner(text="In progress"):
>>> time.sleep(3)
>>> st.success("Done")
# Show and update progress bar
>>> bar = st.progress(50)
>>> time.sleep(3)
>>> bar.progress(100)
>>> with st.status("Authenticating...") as s:
>>> time.sleep(2)
>>> st.write("Some long response.")
>>> s.update(label="Response")
st.balloons()
st.snow()
st.toast("Warming up...")
st.error("Error message")
st.warning("Warning message")
st.info("Info message")
st.success("Success message")
st.exception(e)
Personalize apps for users
# Show different content based on the user's email address.
>>> if st.user.email == "[email protected]":
>>> display_jane_content()
>>> elif st.user.email == "[email protected]":
>>> display_adam_content()
>>> else:
>>> st.write("Please contact us to get access!")
Previous:
Changelog
Next:
Streamlit Community Cloud
forum
Still have questions?
Our forums are full of helpful information and Streamlit experts.
Home
Contact Us
Community
Copyright © 2024, Streamlit Inc.Cookie policy
—————
Additional Streamlit features - Streamlit Docs
https://docs.streamlit.io/get-started/fundamentals/additional-features
—————
st.text_input - Streamlit Docs
https://docs.streamlit.io/library/api-reference/widgets/st.text_input
…
st.text_area - Streamlit Docs
https://docs.streamlit.io/library/api-reference/widgets/st.text_area
—————
st.slider - Streamlit Docs
https://docs.streamlit.io/library/api-reference/widgets/st.slider
Bijvoorbeeld om de temperature voor de LLM in te stellen.
—————
st.radio - Streamlit Docs
https://docs.streamlit.io/library/api-reference/widgets/st.radio
Bijvoorbeeld om uit een paar mogelijkheden 1 keuze te maken.
Misschien enkele talen?
Maar bij een keuze uit heel veel opties, gebruik dan:
VOOR EEN KEUZE UIT HEEL VEEL TALEN:
st.selectbox - Streamlit Docs
https://docs.streamlit.io/library/api-reference/widgets/st.selectbox
!!!!! !!!!
Of eventueel:
st.select_slider - Streamlit Docs
https://docs.streamlit.io/library/api-reference/widgets/st.select_slider
…
—————
Create a multipage app - Streamlit Docs
https://docs.streamlit.io/get-started/tutorials/create-a-multipage-app
—————
st.audio - Streamlit Docs
https://docs.streamlit.io/library/api-reference/media/st.audio
—————
st.image - Streamlit Docs
https://docs.streamlit.io/library/api-reference/media/st.image
—————
st.expander - Streamlit Docs
https://docs.streamlit.io/library/api-reference/layout/st.expander
Bijvoorbeeld:
Om uitklapbare toelichting te geven bij een item. Zoals een overzicht/uitleg bij een prompt/query, of een hele series voorbeeld vragen.
Dit zou ook op een apart tabblad gezet kunnen worden:
st.tabs - Streamlit Docs
https://docs.streamlit.io/library/api-reference/layout/st.tabs
—————
st.sidebar - Streamlit Docs
https://docs.streamlit.io/library/api-reference/layout/st.sidebar
—————
st.tabs - Streamlit Docs
https://docs.streamlit.io/library/api-reference/layout/st.tabs
!!!!! !!!!!
—————
st.status - Streamlit Docs
https://docs.streamlit.io/library/api-reference/status/st.status
Insert a status container to display output from long-running tasks.
—————
|