Spaces:
Runtime error
Runtime error
File size: 9,549 Bytes
4f7e359 |
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 |
{
"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 <module>\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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Student Name</th>\n",
" <th>Marks</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Amelia Kami</td>\n",
" <td>82</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Antoinne Mark</td>\n",
" <td>76</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Peter Zen</td>\n",
" <td>96</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>North Kim</td>\n",
" <td>68</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}
|