{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# helper_utilities.ipynb\n", "> Helper functions for when we need to work with files in Google Colab or locally\n", "\n", "In this notebook, we write some generic code to help us interface more easily with loading in files." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "skip_exec: true\n", "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| default_exp IOHelperUtilities" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "import ipywidgets as widgets\n", "from IPython.display import display, clear_output\n", "from functools import partial\n", "from ipyfilechooser import FileChooser\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "def check_is_colab():\n", " \"\"\"\n", " Check if the current environment is Google Colab.\n", " \"\"\"\n", " try:\n", " import google.colab\n", " return True\n", " except:\n", " return False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert not check_is_colab(), 'On this system, we should not be in Colab'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File Choosers\n", "Jupyter notebooks, the different IDEs, and ipywidgets currently (as of August 8, 2023) are not playing nice together, and also are misaligned in terms of versions. What works in Jupyter Lab at version 8 somehow doesn't work in Google Colab and changes are needed. Neither the Google Colab version or the Jupyter Lab version work with VSCode.\n", "\n", "While this is being worked out between VS Code developers and ipywidgets, we've found a mid-term solutions which requires another package. We implement and test this below (thanks, Code Interpreter!)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "class MultiFileChooser:\n", " def __init__(self):\n", " self.fc = FileChooser('.')\n", " self.fc.title = \"Use the following file chooser to add each file individually.\\n You can remove files by clicking the remove button.\"\n", " self.fc.use_dir_icons = True\n", " self.fc.show_only_dirs = False\n", " self.selected_files = []\n", " \n", " self.fc.register_callback(self.file_selected)\n", " \n", " self.output = widgets.Output()\n", " \n", " def file_selected(self, chooser):\n", " if self.fc.selected is not None and self.fc.selected not in self.selected_files:\n", " self.selected_files.append(self.fc.selected)\n", " self.update_display()\n", " \n", " def update_display(self):\n", " with self.output:\n", " clear_output()\n", " for this_file in self.selected_files:\n", " remove_button = widgets.Button(description=\"Remove\", tooltip=\"Remove this file\")\n", " remove_button.on_click(partial(self.remove_file, file=this_file))\n", " display(widgets.HBox([widgets.Label(value=this_file), remove_button]))\n", " \n", " def remove_file(self, button, this_file):\n", " if this_file in self.selected_files:\n", " self.selected_files.remove(this_file)\n", " self.update_display()\n", " \n", " def display(self):\n", " display(self.fc, self.output)\n", " \n", " def get_selected_files(self):\n", " return self.selected_files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we test the file chooser very briefly to ensure that the results are as we desire." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "07ff01c3633c4addb72bfe758fb9c4e4", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FileChooser(path='/workspaces/lo-achievement/nbs', filename='', title='Use the following file chooser to add e…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1aa36e43d7254a2f8669a92e156b726c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Create file chooser and interact\n", "mfc = MultiFileChooser()\n", "mfc.display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/workspaces/lo-achievement/nbs/_quarto.yml',\n", " '/workspaces/lo-achievement/nbs/nbdev.yml']" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# get files that were selected.\n", "mfc.get_selected_files()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File loading\n", "Now, we implement a file chooser that will work across platforms, whether it be Google Colab or local environments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "def setup_drives(upload_set):\n", "\n", " upload_set = upload_set.lower()\n", " uploaded = None\n", "\n", " # allow them to mount the drive if they chose Google Colab.\n", " if upload_set == 'google drive':\n", " if check_is_colab():\n", " from google.colab import drive\n", " drive.mount('/content/drive')\n", " else:\n", " raise ValueError(\"It looks like you're not on Google Colab. Google Drive mounting is currently only implemented for Google Colab.\")\n", "\n", " # Everything else means that they'll need to use a file chooser (including Google Drive)\n", " if check_is_colab():\n", " from google.colab import files\n", " uploaded = files.upload()\n", " else:\n", " # Create file chooser and interact\n", " mfc = MultiFileChooser()\n", " mfc.display()\n", " uploaded = mfc.get_selected_files()\n", " \n", " return uploaded" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c88d8353fb0d4dc6a2df946ea2082e5f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FileChooser(path='/workspaces/lo-achievement/nbs', filename='', title='Use the following file chooser to add e…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9b8ed072582b4e87ace35d2d59c3a82f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res = setup_drives('local drive')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/workspaces/lo-achievement/nbs/_quarto.yml',\n", " '/workspaces/lo-achievement/nbs/nbdev.yml']" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we'll verify the behavior of Google Drive. We'll wrap this in a try/except block so the code can run all the way through." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "An exception of type ValueError occurred. Arguments:\n", "It looks like you're not on Google Colab. Google Drive mounting is currently only implemented for Google Colab.\n" ] } ], "source": [ "try:\n", " setup_drives('google drive')\n", "except Exception as e:\n", " print(f\"An exception of type {type(e).__name__} occurred. Arguments:\\n{e}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Future expected implementation\n", "\n", "The following code is included as it works, just not in Visual Studio code. The current implementation of the File chooser is a bit inelegant, but this is due to the current limitations of the combination of the libraries and platforms. Once some errors with VS code can be updated, this code will be the preferable solution as it is more familiar to users." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as widgets\n", "from IPython.display import display\n", "\n", "class UniversalFileUpload:\n", "\n", " def __init__(self):\n", " self.filelist = []\n", " self.uploader = None\n", " self.status_output = None\n", " \n", " def _process_upload(self, change):\n", " self.status_output.clear_output()\n", " with self.status_output:\n", " print('What is happening?')\n", " print(change)\n", "\n", " def process_uploads(self, change):\n", " if change['new'] and change['new'] != None:\n", " with self.status_output:\n", " print(change)\n", " \n", " self.filelist = change['new']\n", " \n", " #get filenames and promt\n", " fnames = [fileinfo['name'] for fileinfo in self.filelist['metadata']]\n", " with self.status_output:\n", " print('Uploaded files:', fnames)\n", " \n", " #clear it so it doesn't save state\n", " self.uploader.close()\n", " \n", " def get_upload_value(self):\n", " return self.filelist\n", " \n", " def choose_files(self):\n", " self.uploader = widgets.FileUpload(accept='', multiple=True, description='cat')\n", " self.status_output = widgets.Output()\n", " self.file_output_box = widgets.VBox([self.uploader, self.status_output])\n", " self.uploader.observe(self._process_upload)\n", "\n", " with self.status_output:\n", " print('Waiting...')\n", "\n", " return self.file_output_box" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#test\n", "ul = UniversalFileUpload()\n", "ul.choose_files()" ] } ], "metadata": { "kernelspec": { "display_name": "python3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }