{ "cells": [ { "cell_type": "markdown", "id": "f2054953", "metadata": {}, "source": [ "### Introduction" ] }, { "cell_type": "markdown", "id": "defdb7b1", "metadata": {}, "source": [ "#### Installing streamlit\n", "Streamlit allows fast conversion of data scripts into web apps.\n", "\n", "TO get started you first need to have Streamlit installed. To do so, just run the command below:" ] }, { "cell_type": "code", "execution_count": null, "id": "b89854fb", "metadata": {}, "outputs": [], "source": [ "pip install streamlit\n", "#using conda\n", "conda install streamlit" ] }, { "cell_type": "markdown", "id": "1506fa44", "metadata": {}, "source": [ "#### How to run Streamlit applications\n", "\n", "To execute Streamlit apps, the following command is used:" ] }, { "cell_type": "code", "execution_count": null, "id": "e9c700f5", "metadata": {}, "outputs": [], "source": [ "\n", "file = st.file_uploader(\"Please select a file to upload\")\n", "\n", "if file is not None:\n", "\n", " #Read the file as bytes:\n", "\n", " bytes_df = file.getvalue()\n", "\n", " st.write(bytes_df)\n", "\n", " #Convert the file to a string-based IO:\n", "\n", " stringio = StringIO(file.getvalue().decode(\"utf-8\"))\n", "\n", " st.write(stringio)\n", "\n", " #Read the file as a string file:\n", "\n", " string_df = stringio.read()\n", "\n", " st.write(string_df)\n", "\n", " #Usable whenever a \"file-like\" object is accepted:\n", "\n", " data = pd.read_csv(file)\n", "\n", " st.write(data)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ea8d494d", "metadata": {}, "outputs": [], "source": [ "#Add side widget\n", "\n", "def your_widget(key):\n", " st.subheader('Hi! Welcome')\n", " return st.button(key + \"Step\")\n", "\n", "# Displayed in the main area\n", "clicked = your_widget(\"First\")\n", "\n", "# Shown within an expander\n", "your_expander = st.expander(\"Expand\", expanded=True)\n", "with your_expander:\n", " clicked = your_widget(\"Second\")\n", "\n", "# Shown in the st.sidebar!\n", "with st.sidebar:\n", " clicked = your_widget(\"Last\")\n" ] }, { "cell_type": "markdown", "id": "ad037296", "metadata": {}, "source": [ "### Streamlit Widgets" ] }, { "cell_type": "code", "execution_count": 3, "id": "c05b2e95", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2022-08-07 19:15:58.314 INFO numexpr.utils: NumExpr defaulting to 4 threads.\n" ] } ], "source": [ "import streamlit as st" ] }, { "cell_type": "code", "execution_count": null, "id": "839cd69d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2022-08-08 14:43:23.739 INFO numexpr.utils: NumExpr defaulting to 4 threads.\n", "\u001b[0m\n", "\u001b[34m\u001b[1m You can now view your Streamlit app in your browser.\u001b[0m\n", "\u001b[0m\n", "\u001b[34m Local URL: \u001b[0m\u001b[1mhttp://localhost:8501\u001b[0m\n", "\u001b[34m Network URL: \u001b[0m\u001b[1mhttp://10.16.0.5:8501\u001b[0m\n", "\u001b[0m\n", "app.py:144: UserWarning:\n", "\n", "Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n", "\n", "app.py:144: UserWarning:\n", "\n", "Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n", "\n", "app.py:150: UserWarning:\n", "\n", "Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n", "\n", "app.py:150: UserWarning:\n", "\n", "Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n", "\n", "2022-08-08 14:44:02.778127: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n", "2022-08-08 14:44:02.778176: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n", "2022-08-08 14:44:09.662 Uncaught app exception\n", "Traceback (most recent call last):\n", " File \"/home/kamanda/anaconda/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py\", line 557, in _run_script\n", " exec(code, module.__dict__)\n", " File \"app.py\", line 320, in \n", " from transformers import pipeline\n", "ImportError: cannot import name 'pipeline' from 'transformers' (/home/kamanda/anaconda/lib/python3.9/site-packages/transformers/__init__.py)\n" ] } ], "source": [ " #run an app\n", "!streamlit run app.py" ] }, { "cell_type": "code", "execution_count": 4, "id": "393dfc57", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Student NameMarks
0Amelia Kami82
1Antoinne Mark76
2Peter Zen96
3North Kim68
\n", "
" ], "text/plain": [ " Student Name Marks\n", "0 Amelia Kami 82\n", "1 Antoinne Mark 76\n", "2 Peter Zen 96\n", "3 North Kim 68" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "students = [\"Amelia Kami\", \"Antoinne Mark\", \"Peter Zen\", \"North Kim\"]\n", "\n", "marks = [82, 76, 96, 68]\n", "\n", "import pandas as pd\n", "\n", "df = pd.DataFrame()\n", "\n", "df[\"Student Name\"] = students\n", "\n", "df[\"Marks\"] = marks\n", "df" ] }, { "cell_type": "code", "execution_count": 16, "id": "e0965273", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AgGridReturn(data= Animals Number\n", "0 Zebras 65\n", "1 Elephants 72\n", "2 Rhinos 77\n", "3 Leopards 59, selected_rows=[])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "fig = plt.figure()\n", "ax = fig.add_axes([0,0,1,1])\n", "animals = [\"Zebras\", \"Elephants\", \"Rhinos\", \"Leopards\"]\n", "number = [65, 72, 77, 59]\n", "ax.bar(animals, number)\n", "fig = plt.show()\n", "st.pyplot(fig)\n", "#Seaborn\n", "import seaborn as sns\n", "fig = plt.figure()\n", "ax = sns.barplot(x = animals, y = number)\n", "fig = plt.show()\n", "st.pyplot(fig)\n", "\n", "#Altair\n", "#define data\n", "df = pd.DataFrame()\n", "\n", "df[\"Animals\"] = animals\n", "df[\"Number\"] = number\n", "#Components\n", "from st_aggrid import AgGrid\n", "AgGrid(df)\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "c21f896b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2022-08-07 19:30:27.154 \n", " \u001b[33m\u001b[1mWarning:\u001b[0m to view this Streamlit app on a browser, run it with the following\n", " command:\n", "\n", " streamlit run /home/kamanda/anaconda/lib/python3.9/site-packages/ipykernel_launcher.py [ARGUMENTS]\n" ] } ], "source": [] } ], "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.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }