Skip to content

Commit

Permalink
added show button to show in server
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Skov Madsen authored and Marc Skov Madsen committed Mar 27, 2020
1 parent 69cbf7f commit ce00a21
Showing 1 changed file with 209 additions and 0 deletions.
209 changes: 209 additions & 0 deletions examples/gallery/param/FileUploadWithPandasAndPlots.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fileinput Exploration with Pandas and Plotly\n",
"\n",
"This notebook was contributed to the Panel Gallery by [Marc Skov Madsen](https://datamodelsanalytics.com/) as an answer to the question [A button to upload csv file and plot it ](https://discourse.holoviz.org/t/a-button-to-upload-csv-file-and-plot-it/365) on [Discourse](https://discourse.holoviz.org/)\n",
"\n",
"In Panel the File upload widget is called `Fileinput` and you can find the reference example [here](https://panel.holoviz.org/reference/widgets/FileInput.html#widgets-gallery-fileinput)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import param\n",
"import panel as pn\n",
"import pandas as pd\n",
"import random\n",
"from datetime import datetime, timedelta\n",
"import io\n",
"import plotly.express as px\n",
"\n",
"pn.extension('plotly')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets start out by creating some sample data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class SampleDataApp(param.Parameterized):\n",
" samples = param.Integer(default=40, bounds=(0,100))\n",
" voltage_bounds=param.Range(default=(0,100), bounds=(0,1000))\n",
" time_bounds=param.CalendarDateRange(\n",
" default=(datetime(2020,2,1), datetime(2020,2,26)),\n",
" bounds=(datetime(2020,1,1), datetime(2020,3,26)),\n",
" )\n",
" fub_ids = param.List(default=[\"a1\",\"b1\",\"b2\"])\n",
" \n",
" sample_df = param.DataFrame()\n",
" generate_sample_df = param.Action()\n",
" \n",
" file_name = param.String(default=\"sample_data.csv\")\n",
" save_sample_df = param.Action()\n",
" \n",
" def __init__(self, **params):\n",
" super().__init__(**params)\n",
" \n",
" self.set_sample_df()\n",
" \n",
" self.generate_sample_df = self.set_sample_df\n",
" self.save_sample_df = self.save_sample_data\n",
" \n",
" def set_sample_df(self, event=None):\n",
" start = self.time_bounds[0]\n",
" display(start)\n",
" end = self.time_bounds[1]\n",
" days = (end-start).days\n",
" \n",
" sample_data = {\n",
" \"Time\": [start+timedelta(days=random.uniform(0,days)) for _ in range(0,self.samples)],\n",
" \"Voltage\": [random.uniform(*self.voltage_bounds) for _ in range(0,self.samples)],\n",
" \"FubId\": [random.choice(self.fub_ids) for _ in range(0,self.samples)],\n",
" }\n",
" self.sample_df = pd.DataFrame(sample_data) \n",
" \n",
" \n",
" def save_sample_data(self, event=None):\n",
" if not self.sample_df is None:\n",
" self.sample_df.to_csv(self.file_name, index=False)\n",
" \n",
" def view(self):\n",
" return pn.Param(self)\n",
"\n",
"sample_data_app = SampleDataApp()\n",
"sample_data_app.view()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Click the `Save sample df` button**\n",
"\n",
"Lets test the data can be read"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sample_df = pd.read_csv(sample_data_app.file_name, parse_dates=[\"Time\"])\n",
"sample_df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let define our `VoltageApp`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class VoltageApp(param.Parameterized):\n",
" data = param.DataFrame()\n",
" file_input = param.Parameter()\n",
" show = param.Action()\n",
" \n",
" def __init__(self, **params):\n",
" self.param.file_input.default = pn.widgets.FileInput()\n",
" \n",
" super().__init__(**params)\n",
" \n",
" self.plot_pane = pn.pane.Plotly(height=600, sizing_mode=\"stretch_width\")\n",
" self.show = self._show\n",
" \n",
" \n",
" @param.depends(\"file_input.value\", watch=True)\n",
" def _parse_file_input(self):\n",
" value = self.file_input.value\n",
" string_io = io.StringIO(value.decode(\"utf8\"))\n",
" self.data=pd.read_csv(string_io, parse_dates=[\"Time\"])\n",
" \n",
" def get_plot(self, df):\n",
" assert (\"Voltage\" in df.columns) and (\"Time\" in df.columns), \"no columns voltage and time\"\n",
" df = (df.loc[df['Voltage'] != 'Invalid/Calib']).copy(deep=True)\n",
" df['Voltage'] = df['Voltage'].apply(lambda x: float(x))\n",
" df.reset_index()\n",
"\n",
" traces = []\n",
"\n",
" if \"FubId\" in df.columns:\n",
" return px.scatter(df, x=\"Time\", y=\"Voltage\", color=\"FubId\")\n",
"\n",
" return px.scatter(df, x=\"Time\", y=\"Voltage\")\n",
" \n",
" @param.depends(\"data\", watch=True)\n",
" def _set_plot(self):\n",
" self.plot_pane.object=self.get_plot(self.data)\n",
" \n",
" def _show(self, event=None):\n",
" pn.Column(\n",
" self.file_input,\n",
" self.plot_pane,\n",
" ).show()\n",
" \n",
" def view(self):\n",
" return pn.Column(\n",
" self.file_input,\n",
" self.plot_pane,\n",
" self.param.show,\n",
" )\n",
" \n",
"voltage_app = VoltageApp()\n",
"voltage_app.view()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Todo\n",
"\n",
"- Solve bug: https://github.com/holoviz/panel/issues/1195"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "panel",
"language": "python",
"name": "panel"
},
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit ce00a21

Please sign in to comment.