Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active June 13, 2024 17:15
Show Gist options
  • Save fomightez/277bf08d89a2e8d6e72dd42321af9096 to your computer and use it in GitHub Desktop.
Save fomightez/277bf08d89a2e8d6e72dd42321af9096 to your computer and use it in GitHub Desktop.
FOR SO 78619042/8508004 Showing a dataframe in modern Jupyter https://stackoverflow.com/q/78619042/8508004
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "4e63f4b3-7b3b-424c-bb9d-fe0b6a0577c1",
"metadata": {},
"source": [
"## For SO https://stackoverflow.com/q/78619042/8508004 'How to load data using ipywidgets and make it available for Download'\n",
"\n",
"Developed in sessions launched from [here](https://github.com/fomightez/3Dscatter_plot_mod_playground-binder) and served via the MyBinder service. Go there and click on a 'launch binder' badge."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a0d3490f-c86e-411a-8f88-14b8c616e738",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting openpyxl\n",
" Obtaining dependency information for openpyxl from https://files.pythonhosted.org/packages/30/d0/abcdb0669931be3a98881e6d7851605981693e93a7924061c67d0cd9f292/openpyxl-3.1.4-py2.py3-none-any.whl.metadata\n",
" Downloading openpyxl-3.1.4-py2.py3-none-any.whl.metadata (2.5 kB)\n",
"Collecting et-xmlfile (from openpyxl)\n",
" Obtaining dependency information for et-xmlfile from https://files.pythonhosted.org/packages/96/c2/3dd434b0108730014f1b96fd286040dc3bcb70066346f7e01ec2ac95865f/et_xmlfile-1.1.0-py3-none-any.whl.metadata\n",
" Downloading et_xmlfile-1.1.0-py3-none-any.whl.metadata (1.8 kB)\n",
"Downloading openpyxl-3.1.4-py2.py3-none-any.whl (251 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m251.4/251.4 kB\u001b[0m \u001b[31m7.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)\n",
"Installing collected packages: et-xmlfile, openpyxl\n",
"Successfully installed et-xmlfile-1.1.0 openpyxl-3.1.4\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install openpyxl"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "60e5cee6-49e5-4eef-8d74-67b1c7618ca5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
" Dload Upload Total Spent Left Speed\n",
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n",
"100 3698 100 3698 0 0 13251 0 --:--:-- --:--:-- --:--:-- 13251\n"
]
}
],
"source": [
"# use any example xlsx as staring point; this was near top when I googled 'sample .xlsx file'\n",
"!curl -OL https://github.com/frictionlessdata/datasets/raw/main/files/excel/sample-1-sheet.xlsx"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "929025cc-719a-4dba-8ce5-929b9fa7619c",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "cd9ee0da02774c57993044436fee6657",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<h2>Showing data</h1><h2>'),))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2427a99022834da2a6bf773a73f1fd74",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Button(description='Click to show', layout=Layout(height='auto', width='auto'), style=ButtonStyle())"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b26fa1ca16e54ce9bddbf0cb23954e80",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import ipywidgets as widgets\n",
"from IPython.display import display\n",
"output = widgets.Output()\n",
"import pandas as pd\n",
"def load_data():\n",
" df = pd.read_excel('sample-1-sheet.xlsx',engine='openpyxl',dtype=str)\n",
" with output:\n",
" output.clear_output() # from 'Output widgets: leveraging Jupyter’s display system' https://ipywidgets.readthedocs.io/en/latest/examples/Output%20Widget.html\n",
" display(df)\n",
"def on_sample_data_click(event):\n",
" load_data()\n",
"\n",
"text_caption = widgets.HTML(value=\"<h2>Showing data</h1><h2>\")\n",
"vbox_text = widgets.VBox([text_caption])\n",
"display(vbox_text)\n",
"\n",
"load_sample_data = widgets.Button(description='Click to show', layout=dict(width='auto', height='auto'))\n",
"load_sample_data.on_click(on_sample_data_click)\n",
"display(load_sample_data)\n",
"display(output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1aa3237c-9ac0-4796-9d97-e15b75809e9a",
"metadata": {},
"outputs": [],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment