Created
December 30, 2021 16:39
How to add radio buttons in Streamlit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "84257960", | |
"metadata": {}, | |
"source": [ | |
"# Recipe Objective: How to add radio buttons in streamlit?" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "9eea2e4f", | |
"metadata": {}, | |
"source": [ | |
"Streamlit allows you to add interactivity directly into the app with the help of widgets. You can add radio buttons to Streamlit app using \"st.radio\". It returns a string value. \n", | |
"\n", | |
" Syntax: st.radio(label, options, index, key, help, on_change, args, kwargs)\n", | |
" Parameters: \n", | |
" \n", | |
" label -> A simple label that explains what this radio button is for\n", | |
" \n", | |
" options -> The labels for the radio options. Internally, this will be cast to str by default. The first column is chosen for pandas.DataFrame.\n", | |
" \n", | |
" index -> The index of the preselected option on first render.\n", | |
" \n", | |
" format_func -> This function allows you to change how radio options are displayed. It takes the raw option as an argument and outputs the label that should be displayed for that option. The radio's return value is unaffected by this.\n", | |
" \n", | |
" key -> An optional string or int to be used as a unique key for this widget. If omitted, a key will be generated for the widget based on its content. Multiple widgets of same types cannot share the same key.\n", | |
" \n", | |
" help -> An optional tooltip that gets displayed by the button\n", | |
" \n", | |
" on_change -> An optional callback invoked when this radio's value is changed\n", | |
" \n", | |
" args -> An optional tuple of args that can be passed to the callback\n", | |
" \n", | |
" kwargs -> An optional dictionary of kwargs that can be passed to the callback\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "40d208b3", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#importing required libraries\n", | |
"import streamlit as st\n", | |
"\n", | |
"#adding a radio button\n", | |
"ques = st.radio(\n", | |
" \"Do you like coding?\",\n", | |
" ('Yes','No'))\n", | |
"\n", | |
"#specifying what should be display when the radio button is selected\n", | |
"if ques == 'Yes':\n", | |
" st.write('You like coding.')\n", | |
"else:\n", | |
" st.write(\"You do not like coding.\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5fb45121", | |
"metadata": {}, | |
"source": [ | |
"To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the \"streamlit run appname.py\" command. Once you run the command, the app will automatically open in your default browser." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment