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basic LLM chat app - Streamlit Docs
16
+
17
+ https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps
18
+
19
+
20
+ Build a basic LLM chat app
21
+
22
+ Introduction
23
+
24
+ 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.
25
+
26
+ 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:
27
+
28
+ 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.
29
+ 2. Next, you'll learn how to Build a simple chatbot GUI with streaming.
30
+ 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.
31
+
32
+ Here's a sneak peek of the LLM-powered chatbot GUI with streaming we'll build in this tutorial:
33
+
34
+
35
+ Play around with the above demo to get a feel for what we'll build in this tutorial. A few things to note:
36
+
37
+ * 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.
38
+ * 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.
39
+ * The assistant's responses are streamed to the frontend and are displayed with a different default avatar.
40
+
41
+ Before we start building, let's take a closer look at the chat elements we'll use.
42
+
43
+
44
+ Chat elements
45
+ 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.
46
+
47
+ 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.
48
+
49
+ For an overview of the API, check out this video tutorial by Chanin Nantasenamat (@dataprofessor), a Senior Developer Advocate at Streamlit.
50
+
51
+
52
+
53
+
54
+ Streamlit
55
+ 13,7K abonnees
56
+ Introducing Streamlit Chat Elements
57
+
58
+
59
+
60
+
61
+
62
+
63
+ st.chat_message
64
+ 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.
65
+ 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.
66
+
67
+ Here's an minimal example of how to use st.chat_message to display a welcome message:
68
+
69
+ import streamlit as st with st.chat_message("user"): st.write("Hello πŸ‘‹")
70
+
71
+ 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.
72
+
73
+ 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))
74
+
75
+ 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:
76
+
77
+ import streamlit as st
78
+ import numpy as np
79
+
80
+ message = st.chat_message("assistant") message.write("Hello human") message.bar_chart(np.random.randn(30, 3))
81
+
82
+ So far, we've displayed predefined messages. But what if we want to display messages based on user input?
83
+
84
+
85
+ st.chat_input
86
+ 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:
87
+
88
+ import streamlit as st
89
+ prompt = st.chat_input("Say something")
90
+
91
+ if prompt:
92
+ st.write(f"User has sent the following prompt: {prompt}")
93
+
94
+ Pretty straightforward, right? Now let's combine st.chat_message and st.chat_input to build a bot the mirrors or echoes your input.
95
+
96
+
97
+
98
+ Build a bot that mirrors your input
99
+ 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.
100
+
101
+ First, let's think about the different components we'll need to build our bot:
102
+
103
+ * Two chat message containers to display messages from the user and the bot, respectively.
104
+ * A chat input widget so the user can type in a message.
105
+ * 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).
106
+
107
+
108
+ import streamlit as st st.title("Echo Bot")
109
+ 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"])
110
+
111
+ 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.
112
+
113
+ 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.
114
+
115
+ if prompt := st.chat_input("What is up?"):
116
+ with st.chat_message("user"): st.markdown(prompt) st.session_state.messages.append({"role": "user", "content": prompt})
117
+
118
+ 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.
119
+
120
+ 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.
121
+
122
+ response = f"Echo: {prompt}" with st.chat_message("assistant"): st.markdown(response) st.session_state.messages.append({"role": "assistant", "content": response})
123
+
124
+ Putting it all together, here's the full code for our simple chatbot GUI and the result:
125
+
126
+ View full codeexpand_more
127
+
128
+ import streamlit as st
129
+
130
+ st.title("Echo Bot")
131
+
132
+ # Initialize chat history
133
+ if "messages" not in st.session_state:
134
+ st.session_state.messages = []
135
+
136
+ # Display chat messages from history on app rerun
137
+ for message in st.session_state.messages:
138
+ with st.chat_message(message["role"]):
139
+ st.markdown(message["content"])
140
+
141
+ # React to user input
142
+ if prompt := st.chat_input("What is up?"):
143
+ # Display user message in chat message container
144
+ st.chat_message("user").markdown(prompt)
145
+ # Add user message to chat history
146
+ st.session_state.messages.append({"role": "user", "content": prompt})
147
+
148
+ response = f"Echo: {prompt}"
149
+ # Display assistant response in chat message container
150
+ with st.chat_message("assistant"):
151
+ st.markdown(response)
152
+ # Add assistant response to chat history
153
+ st.session_state.messages.append({"role": "assistant", "content": response})
154
+
155
+
156
+
157
+ 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.
158
+
159
+
160
+
161
+ Build a simple chatbot GUI with streaming
162
+
163
+ 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.
164
+
165
+ 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.
166
+ Let's just copy the code from the previous section and add a few tweaks to it.
167
+
168
+ import streamlit as st
169
+ import random
170
+ import time
171
+ st.title("Simple chat")
172
+ 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})
173
+
174
+ 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.
175
+
176
+ 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.
177
+
178
+ def response_generator():
179
+ 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)
180
+
181
+ 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.
182
+
183
+ with st.chat_message("assistant"):
184
+ response = st.write_stream(response_generator()) st.session_state.messages.append({"role": "assistant", "content": response})
185
+
186
+ 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.
187
+
188
+ Putting it all together, here's the full code for our simple chatbot GUI and the result:
189
+
190
+ View full codeexpand_more
191
+
192
+ import streamlit as st
193
+ import random
194
+ import time
195
+
196
+
197
+ # Streamed response emulator
198
+ def response_generator():
199
+ response = random.choice(
200
+ [
201
+ "Hello there! How can I assist you today?",
202
+ "Hi, human! Is there anything I can help you with?",
203
+ "Do you need help?",
204
+ ]
205
+ )
206
+ for word in response.split():
207
+ yield word + " "
208
+ time.sleep(0.05)
209
+
210
+
211
+ st.title("Simple chat")
212
+
213
+ # Initialize chat history
214
+ if "messages" not in st.session_state:
215
+ st.session_state.messages = []
216
+
217
+ # Display chat messages from history on app rerun
218
+ for message in st.session_state.messages:
219
+ with st.chat_message(message["role"]):
220
+ st.markdown(message["content"])
221
+
222
+ # Accept user input
223
+ if prompt := st.chat_input("What is up?"):
224
+ # Add user message to chat history
225
+ st.session_state.messages.append({"role": "user", "content": prompt})
226
+ # Display user message in chat message container
227
+ with st.chat_message("user"):
228
+ st.markdown(prompt)
229
+
230
+ # Display assistant response in chat message container
231
+ with st.chat_message("assistant"):
232
+ response = st.write_stream(response_generator())
233
+ # Add assistant response to chat history
234
+ st.session_state.messages.append({"role": "assistant", "content": response})
235
+
236
+
237
+
238
+ 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.
239
+
240
+
241
+ Build a ChatGPT-like app
242
+ 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.
243
+
244
+ Install dependencies
245
+ First let's install the dependencies we'll need for this section:
246
+
247
+ pip install openai streamlit
248
+
249
+
250
+ Add OpenAI API key to Streamlit secrets
251
+
252
+ 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:
253
+ OPENAI_API_KEY = "YOUR_API_KEY"
254
+
255
+
256
+ Write the app
257
+ 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.
258
+
259
+ 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)
260
+
261
+ 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:
262
+
263
+ 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})
264
+
265
+ 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.
266
+
267
+ Putting it all together, here's the full code for our ChatGPT-like app and the result:
268
+
269
+ View full codeexpand_more
270
+
271
+ from openai import OpenAI
272
+ import streamlit as st
273
+
274
+ st.title("ChatGPT-like clone")
275
+
276
+ client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
277
+
278
+ if "openai_model" not in st.session_state:
279
+ st.session_state["openai_model"] = "gpt-3.5-turbo"
280
+
281
+ if "messages" not in st.session_state:
282
+ st.session_state.messages = []
283
+
284
+ for message in st.session_state.messages:
285
+ with st.chat_message(message["role"]):
286
+ st.markdown(message["content"])
287
+
288
+ if prompt := st.chat_input("What is up?"):
289
+ st.session_state.messages.append({"role": "user", "content": prompt})
290
+ with st.chat_message("user"):
291
+ st.markdown(prompt)
292
+
293
+ with st.chat_message("assistant"):
294
+ stream = client.chat.completions.create(
295
+ model=st.session_state["openai_model"],
296
+ messages=[
297
+ {"role": m["role"], "content": m["content"]}
298
+ for m in st.session_state.messages
299
+ ],
300
+ stream=True,
301
+ )
302
+ response = st.write_stream(stream)
303
+ st.session_state.messages.append({"role": "assistant", "content": response})
304
+
305
+
306
+ Congratulations! You've built your own ChatGPT-like app in less than 50 lines of code.
307
+ 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. 🎈
308
+
309
+ forum
310
+
311
+ Still have questions?
312
+ Our forums are full of helpful information and Streamlit experts.
313
+
314
+
315
+ …
316
+
317
+
318
+ ZIE OOK:
319
+
320
+ AI-Yash/st-chat: Streamlit Component, for a Chatbot UI
321
+ https://github.com/AI-Yash/st-chat
322
+
323
+
324
+
325
+
326
+ …
327
+
328
+
329
+
330
+ Build an LLM app using LangChain - Streamlit Docs
331
+
332
+ https://docs.streamlit.io/knowledge-base/tutorials/llm-quickstart
333
+
334
+ Build an LLM app using LangChain
335
+
336
+ OpenAI, LangChain, and Streamlit in 18 lines of code
337
+
338
+ 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.
339
+
340
+ This tutorial is adapted from a blog post by Chanin Nantesanamat: LangChain tutorial #1: Build an LLM-powered app in 18 lines of code.
341
+
342
+
343
+ Objectives
344
+ 1. Get an OpenAI key from the end user.
345
+ 2. Validate the user's OpenAI key.
346
+ 3. Get a text prompt from the user.
347
+ 4. Authenticate OpenAI with the user's key.
348
+ 5. Send the user's prompt to OpenAI's API.
349
+ 6. Get a response and display it.
350
+
351
+ Bonus: Deploy the app on Streamlit Community Cloud!
352
+
353
+ Prerequisites
354
+ * Python 3.8+
355
+ * Streamlit
356
+ * LangChain
357
+ * OpenAI API key
358
+
359
+ Setup coding environment
360
+ In your IDE (integrated coding environment), open the terminal and install the following three Python libraries:
361
+
362
+ pip install streamlit openai langchain
363
+
364
+ 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.
365
+
366
+ streamlit openai langchain
367
+
368
+
369
+ Building the app
370
+ The app is only 18 lines of code:
371
+
372
+ 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)
373
+
374
+
375
+ To start, create a new Python file and save it asΒ streamlit_app.py in the root of your working directory.
376
+
377
+ 1. Import the necessary Python libraries.
import streamlit as st from langchain.llms import OpenAI 

378
+ 2. Create the app's title using st.title.
st.title('πŸ¦œπŸ”— Quickstart App') 

379
+ 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') 

380
+ 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)) 

381
+ 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) 

382
+ 6. Remember to save your file!
383
+ 7. Return to your computer's terminal to run the app.
streamlit run streamlit_app.py 

384
+
385
+ Deploying the app
386
+ To deploy the app to the Streamlit Cloud, follow these steps:
387
+ 1. Create a GitHub repository for the app. Your repository should contain two files:
your-repository/ β”œβ”€β”€ streamlit_app.py └── requirements.txt 

388
+ 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.
389
+ 3. Click theΒ Deploy!Β button.
390
+
391
+ Your app will now be deployed to Streamlit Community Cloud and can be accessed from around the world! 🌎
392
+
393
+
394
+
395
+ Conclusion
396
+ 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.
397
+
398
+ We hope you found this tutorial helpful! Check out more examples to see the power of Streamlit and LLM. πŸ’–
399
+ Happy Streamlit-ing! 🎈
400
+ forum
401
+
402
+ Still have questions?
403
+ Our forums are full of helpful information and Streamlit experts.
404
+
405
+
406
+
407
+
408
+
409
+
410
+ β€”β€”β€”β€”β€”
411
+
412
+ Cheat sheet - Streamlit Docs
413
+ https://docs.streamlit.io/library/cheatsheet
414
+
415
+
416
+ Cheat Sheet
417
+
418
+ This is a summary of the docs, as of Streamlit v1.31.0.
419
+ Install & Import
420
+ pip install streamlit
421
+
422
+ streamlit run first_app.py
423
+
424
+ # Import convention
425
+ >>> import streamlit as st
426
+
427
+ Pre-release features
428
+ pip uninstall streamlit
429
+ pip install streamlit-nightly --upgrade
430
+ Learn more about experimental features
431
+
432
+ Command line
433
+ streamlit --help
434
+ streamlit run your_script.py
435
+ streamlit hello
436
+ streamlit config show
437
+ streamlit cache clear
438
+ streamlit docs
439
+ streamlit --version
440
+
441
+ Magic commands
442
+ # Magic commands implicitly
443
+ # call st.write().
444
+ "_This_ is some **Markdown***"
445
+ my_variable
446
+ "dataframe:", my_data_frame
447
+
448
+ Display text
449
+ st.write("Most objects") # df, err, func, keras!
450
+ st.write(["st", "is <", 3]) # see *
451
+ st.write_stream(my_generator)
452
+ st.write_stream(my_llm_stream)
453
+
454
+ st.text("Fixed width text")
455
+ st.markdown("_Markdown_") # see *
456
+ st.latex(r""" e^{i\pi} + 1 = 0 """)
457
+ st.title("My title")
458
+ st.header("My header")
459
+ st.subheader("My sub")
460
+ st.code("for i in range(8): foo()")
461
+ * optional kwarg unsafe_allow_html = True
462
+
463
+ Display data
464
+ st.dataframe(my_dataframe)
465
+ st.table(data.iloc[0:10])
466
+ st.json({"foo":"bar","fu":"ba"})
467
+ st.metric("My metric", 42, 2)
468
+
469
+ Display media
470
+ st.image("./header.png")
471
+ st.audio(data)
472
+ st.video(data)
473
+ st.video(data, subtitles="./subs.vtt")
474
+
475
+ Display charts
476
+ st.area_chart(df)
477
+ st.bar_chart(df)
478
+ st.line_chart(df)
479
+ st.map(df)
480
+ st.scatter_chart(df)
481
+
482
+ st.altair_chart(chart)
483
+ st.bokeh_chart(fig)
484
+ st.graphviz_chart(fig)
485
+ st.plotly_chart(fig)
486
+ st.pydeck_chart(chart)
487
+ st.pyplot(fig)
488
+ st.vega_lite_chart(df)
489
+
490
+ Add widgets to sidebar
491
+ # Just add it after st.sidebar:
492
+ >>> a = st.sidebar.radio("Select one:", [1, 2])
493
+
494
+ # Or use "with" notation:
495
+ >>> with st.sidebar:
496
+ >>> st.radio("Select one:", [1, 2])
497
+
498
+ Columns
499
+ # Two equal columns:
500
+ >>> col1, col2 = st.columns(2)
501
+ >>> col1.write("This is column 1")
502
+ >>> col2.write("This is column 2")
503
+
504
+ # Three different columns:
505
+ >>> col1, col2, col3 = st.columns([3, 1, 1])
506
+ # col1 is larger.
507
+
508
+ # You can also use "with" notation:
509
+ >>> with col1:
510
+ >>> st.radio("Select one:", [1, 2])
511
+
512
+ Tabs
513
+ # Insert containers separated into tabs:
514
+ >>> tab1, tab2 = st.tabs(["Tab 1", "Tab2"])
515
+ >>> tab1.write("this is tab 1")
516
+ >>> tab2.write("this is tab 2")
517
+
518
+ # You can also use "with" notation:
519
+ >>> with tab1:
520
+ >>> st.radio("Select one:", [1, 2])
521
+
522
+ Expandable containers
523
+ >>> expand = st.expander("My label")
524
+ >>> expand.write("Inside the expander.")
525
+ >>> pop = st.popover("Button label")
526
+ >>> pop.checkbox("Show all")
527
+
528
+ # You can also use "with" notation:
529
+ >>> with expand:
530
+ >>> st.radio("Select one:", [1, 2])
531
+
532
+ Control flow
533
+ # Stop execution immediately:
534
+ st.stop()
535
+ # Rerun script immediately:
536
+ st.rerun()
537
+ # Navigate to another page:
538
+ st.switch_page("pages/my_page.py")
539
+
540
+ # Group multiple widgets:
541
+ >>> with st.form(key="my_form"):
542
+ >>> username = st.text_input("Username")
543
+ >>> password = st.text_input("Password")
544
+ >>> st.form_submit_button("Login")
545
+
546
+ Display interactive widgets
547
+ st.button("Click me")
548
+ st.download_button("Download file", data)
549
+ st.link_button("Go to gallery", url)
550
+ st.page_link("app.py", label="Home")
551
+ st.data_editor("Edit data", data)
552
+ st.checkbox("I agree")
553
+ st.toggle("Enable")
554
+ st.radio("Pick one", ["cats", "dogs"])
555
+ st.selectbox("Pick one", ["cats", "dogs"])
556
+ st.multiselect("Buy", ["milk", "apples", "potatoes"])
557
+ st.slider("Pick a number", 0, 100)
558
+ st.select_slider("Pick a size", ["S", "M", "L"])
559
+ st.text_input("First name")
560
+ st.number_input("Pick a number", 0, 10)
561
+ st.text_area("Text to translate")
562
+ st.date_input("Your birthday")
563
+ st.time_input("Meeting time")
564
+ st.file_uploader("Upload a CSV")
565
+ st.camera_input("Take a picture")
566
+ st.color_picker("Pick a color")
567
+
568
+ # Use widgets' returned values in variables:
569
+ >>> for i in range(int(st.number_input("Num:"))):
570
+ >>> foo()
571
+ >>> if st.sidebar.selectbox("I:",["f"]) == "f":
572
+ >>> b()
573
+ >>> my_slider_val = st.slider("Quinn Mallory", 1, 88)
574
+ >>> st.write(slider_val)
575
+
576
+ # Disable widgets to remove interactivity:
577
+ >>> st.slider("Pick a number", 0, 100, disabled=True)
578
+
579
+ Build chat-based apps
580
+ # Insert a chat message container.
581
+ >>> with st.chat_message("user"):
582
+ >>> st.write("Hello πŸ‘‹")
583
+ >>> st.line_chart(np.random.randn(30, 3))
584
+
585
+ # Display a chat input widget at the bottom of the app.
586
+ >>> st.chat_input("Say something")
587
+
588
+ # Display a chat input widget inline.
589
+ >>> with st.container():
590
+ >>> st.chat_input("Say something")
591
+ Learn how to Build a basic LLM chat app
592
+
593
+ Mutate data
594
+ # Add rows to a dataframe after
595
+ # showing it.
596
+ >>> element = st.dataframe(df1)
597
+ >>> element.add_rows(df2)
598
+
599
+ # Add rows to a chart after
600
+ # showing it.
601
+ >>> element = st.line_chart(df1)
602
+ >>> element.add_rows(df2)
603
+
604
+ Display code
605
+ >>> with st.echo():
606
+ >>> st.write("Code will be executed and printed")
607
+
608
+ Placeholders, help, and options
609
+ # Replace any single element.
610
+ >>> element = st.empty()
611
+ >>> element.line_chart(...)
612
+ >>> element.text_input(...) # Replaces previous.
613
+
614
+ # Insert out of order.
615
+ >>> elements = st.container()
616
+ >>> elements.line_chart(...)
617
+ >>> st.write("Hello")
618
+ >>> elements.text_input(...) # Appears above "Hello".
619
+
620
+ st.help(pandas.DataFrame)
621
+ st.get_option(key)
622
+ st.set_option(key, value)
623
+ st.set_page_config(layout="wide")
624
+ st.query_params[key]
625
+ st.query_params.get_all(key)
626
+ st.query_params.clear()
627
+
628
+ Connect to data sources
629
+ st.connection("pets_db", type="sql")
630
+ conn = st.connection("sql")
631
+ conn = st.connection("snowflake")
632
+
633
+ >>> class MyConnection(BaseConnection[myconn.MyConnection]):
634
+ >>> def _connect(self, **kwargs) -> MyConnection:
635
+ >>> return myconn.connect(**self._secrets, **kwargs)
636
+ >>> def query(self, query):
637
+ >>> return self._instance.query(query)
638
+
639
+ Optimize performance
640
+ Cache data objects
641
+ # E.g. Dataframe computation, storing downloaded data, etc.
642
+ >>> @st.cache_data
643
+ ... def foo(bar):
644
+ ... # Do something expensive and return data
645
+ ... return data
646
+ # Executes foo
647
+ >>> d1 = foo(ref1)
648
+ # Does not execute foo
649
+ # Returns cached item by value, d1 == d2
650
+ >>> d2 = foo(ref1)
651
+ # Different arg, so function foo executes
652
+ >>> d3 = foo(ref2)
653
+ # Clear all cached entries for this function
654
+ >>> foo.clear()
655
+ # Clear values from *all* in-memory or on-disk cached functions
656
+ >>> st.cache_data.clear()
657
+ Cache global resources
658
+ # E.g. TensorFlow session, database connection, etc.
659
+ >>> @st.cache_resource
660
+ ... def foo(bar):
661
+ ... # Create and return a non-data object
662
+ ... return session
663
+ # Executes foo
664
+ >>> s1 = foo(ref1)
665
+ # Does not execute foo
666
+ # Returns cached item by reference, s1 == s2
667
+ >>> s2 = foo(ref1)
668
+ # Different arg, so function foo executes
669
+ >>> s3 = foo(ref2)
670
+ # Clear all cached entries for this function
671
+ >>> foo.clear()
672
+ # Clear all global resources from cache
673
+ >>> st.cache_resource.clear()
674
+ Deprecated caching
675
+ >>> @st.cache
676
+ ... def foo(bar):
677
+ ... # Do something expensive in here...
678
+ ... return data
679
+ >>> # Executes foo
680
+ >>> d1 = foo(ref1)
681
+ >>> # Does not execute foo
682
+ >>> # Returns cached item by reference, d1 == d2
683
+ >>> d2 = foo(ref1)
684
+ >>> # Different arg, so function foo executes
685
+ >>> d3 = foo(ref2)
686
+
687
+ Display progress and status
688
+ # Show a spinner during a process
689
+ >>> with st.spinner(text="In progress"):
690
+ >>> time.sleep(3)
691
+ >>> st.success("Done")
692
+
693
+ # Show and update progress bar
694
+ >>> bar = st.progress(50)
695
+ >>> time.sleep(3)
696
+ >>> bar.progress(100)
697
+
698
+ >>> with st.status("Authenticating...") as s:
699
+ >>> time.sleep(2)
700
+ >>> st.write("Some long response.")
701
+ >>> s.update(label="Response")
702
+
703
+ st.balloons()
704
+ st.snow()
705
+ st.toast("Warming up...")
706
+ st.error("Error message")
707
+ st.warning("Warning message")
708
+ st.info("Info message")
709
+ st.success("Success message")
710
+ st.exception(e)
711
+
712
+ Personalize apps for users
713
+ # Show different content based on the user's email address.
714
+ >>> if st.user.email == "jane@email.com":
715
+ >>> display_jane_content()
716
+ >>> elif st.user.email == "adam@foocorp.io":
717
+ >>> display_adam_content()
718
+ >>> else:
719
+ >>> st.write("Please contact us to get access!")
720
+
721
+ Previous:
722
+ Changelog
723
+ Next:
724
+ Streamlit Community Cloud
725
+
726
+ forum
727
+ Still have questions?
728
+ Our forums are full of helpful information and Streamlit experts.
729
+
730
+ Home
731
+ Contact Us
732
+ Community
733
+
734
+
735
+
736
+
737
+
738
+
739
+
740
+
741
+
742
+ Copyright Β© 2024, Streamlit Inc.Cookie policy
743
+
744
+
745
+
746
+ β€”β€”β€”β€”β€”
747
+
748
+
749
+ Additional Streamlit features - Streamlit Docs
750
+
751
+ https://docs.streamlit.io/get-started/fundamentals/additional-features
752
+
753
+
754
+ β€”β€”β€”β€”β€”
755
+
756
+ st.text_input - Streamlit Docs
757
+
758
+ https://docs.streamlit.io/library/api-reference/widgets/st.text_input
759
+
760
+
761
+
762
+
763
+ …
764
+
765
+
766
+ st.text_area - Streamlit Docs
767
+
768
+ https://docs.streamlit.io/library/api-reference/widgets/st.text_area
769
+
770
+
771
+
772
+
773
+
774
+ β€”β€”β€”β€”β€”
775
+
776
+ st.slider - Streamlit Docs
777
+
778
+ https://docs.streamlit.io/library/api-reference/widgets/st.slider
779
+
780
+
781
+ Bijvoorbeeld om de temperature voor de LLM in te stellen.
782
+
783
+
784
+
785
+
786
+ β€”β€”β€”β€”β€”
787
+
788
+ st.radio - Streamlit Docs
789
+
790
+ https://docs.streamlit.io/library/api-reference/widgets/st.radio
791
+
792
+
793
+ Bijvoorbeeld om uit een paar mogelijkheden 1 keuze te maken.
794
+ Misschien enkele talen?
795
+
796
+ Maar bij een keuze uit heel veel opties, gebruik dan:
797
+
798
+
799
+ VOOR EEN KEUZE UIT HEEL VEEL TALEN:
800
+ st.selectbox - Streamlit Docs
801
+
802
+ https://docs.streamlit.io/library/api-reference/widgets/st.selectbox
803
+ !!!!! !!!!
804
+
805
+
806
+ Of eventueel:
807
+
808
+ st.select_slider - Streamlit Docs
809
+
810
+ https://docs.streamlit.io/library/api-reference/widgets/st.select_slider
811
+
812
+
813
+ …
814
+
815
+
816
+
817
+ β€”β€”β€”β€”β€”
818
+
819
+ Create a multipage app - Streamlit Docs
820
+
821
+ https://docs.streamlit.io/get-started/tutorials/create-a-multipage-app
822
+
823
+
824
+ β€”β€”β€”β€”β€”
825
+
826
+ st.audio - Streamlit Docs
827
+
828
+ https://docs.streamlit.io/library/api-reference/media/st.audio
829
+
830
+
831
+
832
+
833
+ β€”β€”β€”β€”β€”
834
+
835
+ st.image - Streamlit Docs
836
+
837
+ https://docs.streamlit.io/library/api-reference/media/st.image
838
+
839
+
840
+ β€”β€”β€”β€”β€”
841
+
842
+ st.expander - Streamlit Docs
843
+ https://docs.streamlit.io/library/api-reference/layout/st.expander
844
+
845
+ Bijvoorbeeld:
846
+ Om uitklapbare toelichting te geven bij een item. Zoals een overzicht/uitleg bij een prompt/query, of een hele series voorbeeld vragen.
847
+ Dit zou ook op een apart tabblad gezet kunnen worden:
848
+
849
+
850
+ st.tabs - Streamlit Docs
851
+
852
+ https://docs.streamlit.io/library/api-reference/layout/st.tabs
853
+
854
+
855
+ β€”β€”β€”β€”β€”
856
+
857
+
858
+ st.sidebar - Streamlit Docs
859
+
860
+ https://docs.streamlit.io/library/api-reference/layout/st.sidebar
861
+
862
+
863
+ β€”β€”β€”β€”β€”
864
+
865
+ st.tabs - Streamlit Docs
866
+
867
+ https://docs.streamlit.io/library/api-reference/layout/st.tabs
868
+ !!!!! !!!!!
869
+
870
+
871
+ β€”β€”β€”β€”β€”
872
+
873
+
874
+ st.status - Streamlit Docs
875
+
876
+ https://docs.streamlit.io/library/api-reference/status/st.status
877
+
878
+ Insert a status container to display output from long-running tasks.
879
+
880
+
881
+ β€”β€”β€”β€”β€”
882
+
883
+