{"id":693,"date":"2022-01-18T13:35:00","date_gmt":"2022-01-18T08:05:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=693"},"modified":"2023-08-15T17:25:44","modified_gmt":"2023-08-15T11:55:44","slug":"python-web-app-under-100-lines-of-code-using-streamlit","status":"publish","type":"post","link":"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit","title":{"rendered":"How to Create a Webapp Using Streamlit in Python &#8211; Covid19 Webapp"},"content":{"rendered":"\n<p>Less than 100 lines of code, I made a web app with just under 100 lines of code using&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/streamlit.io\/\" target=\"_blank\">Streamlit<\/a>,&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/chart-studio.plotly.com\/feed\/#\/\" target=\"_blank\">Plotly<\/a>,&nbsp;and&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/pandas.pydata.org\/\" target=\"_blank\">Pandas<\/a>&nbsp;which is an open-source data analysis and manipulation tool, built on top of the Python programming language.<\/p>\n\n\n\n<p>Well, if you are not aware of what is Streamlit then I will give you a quick idea about Streamlit.<\/p>\n\n\n\n<p><strong>Streamlit<\/strong>&nbsp;is an open-source app framework that allows you to create interactive and dynamic web apps without even knowing any frontend frameworks.<\/p>\n\n\n\n<p>Let&#8217;s get back to the topic, I am gonna walk you through the project so that you all can easily understand the code and hope to make a better web app by yourself.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-introduction\"><a href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit#heading-introduction\"><\/a>Introduction<\/h1>\n\n\n\n<p>The web app is all about&nbsp;<strong>Exploratory<\/strong>&nbsp;<strong>Data<\/strong>&nbsp;<strong>Analysis<\/strong>&nbsp;and&nbsp;<strong>Visualization<\/strong>&nbsp;of&nbsp;<strong>Covid-19<\/strong>&nbsp;data from India in the year 2020.<\/p>\n\n\n\n<p>Well, I found the data on&nbsp;<a target=\"_blank\" href=\"https:\/\/www.kaggle.com\/\" rel=\"noreferrer noopener\">Kaggle<\/a>. You can download any kind of dataset for free on Kaggle.<\/p>\n\n\n\n<p>Let&#8217;s get back to the track, we gonna have a nice interactive user interface with an interactive sidebar where we&#8217;ll have some user inputs to select the type of graphs to visualize data.<\/p>\n\n\n\n<p>Last but not least, we gonna display reports like which state has the highest cases or lowest cases, etc.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1351\" height=\"609\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/banner.png\" alt=\"\" class=\"wp-image-701\"\/><\/figure>\n\n\n\n<p>Now the talk is over, let&#8217;s head over to the code. Let&#8217;s get started.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-1-setting-up-project\"><a href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit#heading-1-setting-up-project\"><\/a>1. Setting up the Project<\/h1>\n\n\n\n<p>First of all, make sure&nbsp;<a target=\"_blank\" href=\"https:\/\/www.python.org\/\" rel=\"noreferrer noopener\">Python<\/a>&nbsp;is installed on your system.<\/p>\n\n\n\n<p>Now open up your favorite IDE or Code Editor and start an empty project.<\/p>\n\n\n\n<p>Make a directory and then fire the terminal in the code editor or IDE, then install the necessary libraries which are used in this project.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">pip install streamlit pandas plotly<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1052\" height=\"262\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/teminal.png\" alt=\"\" class=\"wp-image-705\"\/><\/figure>\n\n\n\n<p>Now check if the streamlit is successfully installed on your system just run the following command in the terminal.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">streamlit hello<\/pre><\/div>\n\n\n\n<p>If everything is well and good then head over to the next step.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-2-cloning-the-project\"><a href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit#heading-2-cloning-the-project\"><\/a>2. Cloning the Project<\/h1>\n\n\n\n<p>To clone the project head over to my Github profile and copy the repository URL.<\/p>\n\n\n\n<p>Visit this&nbsp;<a target=\"_blank\" href=\"https:\/\/github.com\/Sachin-crypto\/Streamlit-Apps\" rel=\"noreferrer noopener\">repository<\/a>&nbsp;to copy the URL and then run the following command in the terminal.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"962\" height=\"329\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/repo_url.png\" alt=\"\" class=\"wp-image-706\"\/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">git clone repository_url_here<\/pre><\/div>\n\n\n\n<p><strong>Note<\/strong>: make sure the&nbsp;<strong>git<\/strong>&nbsp;is installed on your system.<\/p>\n\n\n\n<p>If you don&#8217;t know <code>git<\/code> then don&#8217;t worry, simply download the project as a ZIP file and extract it to the directory.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"965\" height=\"328\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/download_repo.png\" alt=\"\" class=\"wp-image-707\"\/><\/figure>\n\n\n\n<p>If every step is carried out successfully then proceed to the next step.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-3-understanding-the-code\"><a href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit#heading-3-understanding-the-code\"><\/a>3. Understanding the Code<\/h1>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\"># Importing neccessary libraries\nimport streamlit as st\nimport pandas as pd\nimport plotly.express as px\n\nst.title(\"Covid-19 EDA and Visualization\")\n\nst.markdown('''\nA Web App to visualize and analyze the Covid-19 data from India\n* **Libraries Used:** Streamlit, Pandas, Plotly\n* **Data Source:** Kaggle\n''')\n# Inserting Image\nst.image('Covid-Banner.png', caption=\"Image by Sachin\")\n# Reading csv data\ndata = pd.read_csv('Covid-19.csv')\n# Displaying Data and its Shape\nst.write(\"**Covid-19 dataset**\", data)\nst.write(\"Shape of data\", data.shape)\n# Header of sidebar\nst.sidebar.header(\"User Input\")\n# Creating selectbox for Graphs &amp; Plots\ngraphs = st.sidebar.selectbox(\"Graphs &amp; Plots\", (\"Bar Graph\", \"Scatter Plot\", \"HeatMap\", \"Pie Chart\"))\n# Sorting the columns\nindex = sorted(data.columns.unique())\n# Setting default value for x, y, and color\ndefault_index_x = index.index('State\/UTs')\ndefault_index_y = index.index('Total Cases')\ndefault_index_col = index.index('Death Ratio (%)')\n\n\n# Creating selectbox for x, y and color label and setting default value\nx_label = st.sidebar.selectbox(\"X label Parameter\", index, index=default_index_x)\ny_label = st.sidebar.selectbox(\"Y label Parameter\", index, index=default_index_y)\ncol = st.sidebar.selectbox(\"Color\", index, index=default_index_col)\n\n\nst.markdown('''\n## **Visualization**\n''')\n# function to plot graphs\ndef visualize_plotly(graph):\n    if graph == \"Bar Graph\":\n        st.write(graph)\n        fig = px.bar(data, x=x_label, y=y_label, color=col)\n\n    elif graph == \"Scatter Plot\":\n        st.write(graph)\n        fig = px.scatter(data, x=x_label, y=y_label, color=col)\n\n    elif graph == \"HeatMap\":\n        st.write(graph)\n        fig = px.density_heatmap(data, x=x_label, y=y_label, nbinsx=20, nbinsy=20)\n\n    else:\n        st.write(graph)\n        fig = px.pie(data, values=x_label, names=data[y_label])\n\n    return fig\n\nfigure = visualize_plotly(graphs)\n\nst.plotly_chart(figure)\n\nst.markdown('''\n## **Report**\n''')\n# Creating buttons to display reports\nif st.button(\"Highest Cases\"):\n    st.header(\"Highest Cases in a State\/UT\")\n    highest_cases = data[data['Total Cases'] == max(data['Total Cases'])]\n    st.write(highest_cases)\n\nif st.button(\"Lowest Cases\"):\n    st.header(\"Lowest Cases in a State\/UT\")\n    lowest_cases = data[data['Total Cases'] == min(data['Total Cases'])]\n    st.write(lowest_cases)\n\nif st.button(\"Highest Active Cases\"):\n    st.header(\"Highest Active Cases in a State\/UT\")\n    high_active_cases = data[data['Active'] == max(data['Active'])]\n    st.write(high_active_cases)\n\nif st.button(\"Lowest Active Cases\"):\n    st.header(\"Lowest Active Cases in a State\/UT\")\n    low_active_cases = data[data['Total Cases'] == min(data['Total Cases'])]\n    st.write(low_active_cases)\n\nif st.button(\"Highest Death Ratio (%)\"):\n    st.header(\"Highest Death Ratio (%) in a State\/UT\")\n    high_death = data[data['Death Ratio (%)'] == max(data['Death Ratio (%)'])]\n    st.write(high_death)\n\nif st.button(\"Lowest Death Ratio (%)\"):\n    st.header(\"Lowest Death Ratio (%) in a State\/UT\")\n    low_death = data[data['Death Ratio (%)'] == min(data['Death Ratio (%)'])]\n    st.write(low_death)<\/pre><\/div>\n\n\n\n<p><strong>Line 1 &#8211; 4<\/strong>&nbsp;&#8212; Importing the libraries which are used in the project.<\/p>\n\n\n\n<p><strong>Line 6 &#8211; 12<\/strong>&nbsp;&#8212; By using streamlit&#8217;s title and markdown feature, we are setting up the title and some description about the project.<\/p>\n\n\n\n<p><strong>Line 13 &#8211; 19<\/strong>&nbsp;&#8212; In that portion, we are inserting the banner image using streamlit&#8217;s image feature and reading the&nbsp;<strong>Covid-19<\/strong>&nbsp;data using&nbsp;<strong>Pandas<\/strong>&nbsp;library, and then finally displaying it on the web app.<\/p>\n\n\n\n<p><strong>Line 20 &#8211; 29<\/strong>&nbsp;&#8212; Creating a sidebar and select boxes in which users can select different graphs and plots. Additionally sorting the columns and setting the default values for the x-axis, y-axis, and color parameters.<\/p>\n\n\n\n<p><strong>Line 32 &#8211; 35<\/strong>&nbsp;&#8212; Creating the select boxes for the x-axis, y-axis, and color labels using&nbsp;<code>st.sidebar.selectbox<\/code>&nbsp;and assigning the default values to them which we created just above.<\/p>\n\n\n\n<p><strong>Line 38 &#8211; 63<\/strong>&nbsp;&#8212; Setting a heading for visualization and creating a function to show different-different graphs. Here, I created a function&nbsp;<code>visualize_plotly(graph)<\/code>&nbsp;which takes&nbsp;<code>graph<\/code>&nbsp;and declared&nbsp;<code>if-else<\/code>&nbsp;conditions in which if the user will click on&nbsp;<strong>Bar Graph<\/strong>&nbsp;then it will visualize data on the bar graph and the same goes for other graphs also. Then creating a variable&nbsp;<code>figure<\/code>&nbsp;to call the function&nbsp;<code>visualize_plotly()<\/code>&nbsp;and passing the&nbsp;<code>graphs<\/code>&nbsp;variable from line 23.<\/p>\n\n\n\n<p>Then using&nbsp;<code>st.plotly_chart(figure)<\/code>&nbsp;to display graphs.<\/p>\n\n\n\n<p><strong>Line 65 &#8211; 97<\/strong>&nbsp;&#8212; Creating a report section where we created buttons&nbsp;<code>st.button<\/code>&nbsp;using streamlit and displaying it on web app.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-4-run-the-script\"><a href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit#heading-4-run-the-script\"><\/a>4. Run the Script<\/h1>\n\n\n\n<p>Cool, we are at the end of this project. Now let&#8217;s see how our web app looks like.<\/p>\n\n\n\n<p>Run the following command in your terminal<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">streamlit run your_project_name<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/python-web-app-under-100-lines-of-code-using-streamlit#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>This web app is based on the older stats, you can try with a newer dataset on Covid-19.<\/p>\n\n\n\n<p>It is a beginner-friendly project or intermediate level to some extent, but you can take it to an advanced level by adding different datasets or adding more graphs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/abc-in-python\" rel=\"noreferrer noopener\">What is ABC of Python &#8211; Detailed guide on abstract base classes<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/displaying-images-on-the-frontend-using-fastapi\" rel=\"noreferrer noopener\">Displaying and Uploading images on the frontend using FastAPI<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/init-and-call-method\" rel=\"noreferrer noopener\">What are __init__ and __call__ functions and what do they do<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/build-api-using-fastapi\" rel=\"noreferrer noopener\">Build APIs using the FastAPI web framework in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/integrate-postgresql-database-in-python\" rel=\"noreferrer noopener\">Connect PostgreSQL database using psycopg2 in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/data-augmentation-in-deep-learning\" rel=\"noreferrer noopener\">Tackle the shortage of data using data augmentation techniques<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/one-liners-in-python\" rel=\"noreferrer noopener\">Powerful one-liners in Python to boost the code<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Less than 100 lines of code, I made a web app with just under 100 lines of code using&nbsp;Streamlit,&nbsp;Plotly,&nbsp;and&nbsp;Pandas&nbsp;which is an open-source data analysis and manipulation tool, built on top of the Python programming language. Well, if you are not aware of what is Streamlit then I will give you a quick idea about Streamlit. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":699,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,34,33],"tags":[28,12,35],"class_list":["post-693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-streamlit","category-webapp","tag-project","tag-python","tag-streamlit","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":13,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":1388,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/693\/revisions\/1388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/699"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}