From a329fc39486cdae2c2183df06265763fe5da50c3 Mon Sep 17 00:00:00 2001 From: Namita Shah Date: Fri, 5 Apr 2024 09:41:06 -0700 Subject: [PATCH] 03 Copy File Upload/Download edits --- nbs/03_Copy.ipynb | 272 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 nbs/03_Copy.ipynb diff --git a/nbs/03_Copy.ipynb b/nbs/03_Copy.ipynb new file mode 100644 index 0000000..d304f4c --- /dev/null +++ b/nbs/03_Copy.ipynb @@ -0,0 +1,272 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d09b853a-3124-4232-9618-3b6761678bc5", + "metadata": {}, + "outputs": [], + "source": [ + "#|default_exp file_viewer" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "d8e6a32e-e248-4ec4-9763-7a18c8cd0b9b", + "metadata": {}, + "outputs": [], + "source": [ + "#|export\n", + "import os\n", + "import ipywidgets as widgets\n", + "from ipywidgets import VBox, HBox, HTML, Button, Label, Text, Checkbox, Accordion, FileUpload\n", + "from IPython.display import display, clear_output, FileLink, FileLinks\n", + "import ipyvuetify as v\n", + "from traitlets import observe, HasTraits, Unicode\n", + "from ipyfilechooser import FileChooser\n", + "from langchain_community.document_loaders import PyPDFLoader\n", + "from langchain_community.vectorstores import FAISS\n", + "from langchain_openai import OpenAIEmbeddings\n", + "from jupyter_mentor.llm import LLM, FileModel" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "960e1137-3a29-477a-8b92-408ae003b886", + "metadata": {}, + "outputs": [], + "source": [ + "# File Uploader\n", + "class FileUploader(widgets.VBox):\n", + " def __init__(self):\n", + " super().__init__()\n", + " self.file_upload = widgets.FileUpload(\n", + " accept='.pdf', \n", + " multiple=True # multiple files\n", + " )\n", + " self.children = [self.file_upload]" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "4c5404b9-f2ef-4c28-b210-ae1d9019feb0", + "metadata": {}, + "outputs": [], + "source": [ + "class FileDownloader(widgets.VBox):\n", + " def __init__(self):\n", + " super().__init__()\n", + " self.file_chooser = FileChooser()\n", + " self.download_button = v.Btn(\n", + " class_=\"ma-2\",\n", + " outlined=True,\n", + " href=\"./course_files/hussey2015.pdf\",\n", + " attributes={\"download\": \"hussey2015.pdf\"},\n", + " children=[\"Download All\"]\n", + " )\n", + " self.children = [self.file_chooser, self.download_button]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "07c50690-0a66-4b72-b819-6fd06b1c9016", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "class FileViewerView(Accordion):\n", + " \n", + " def __init__(self):\n", + " super().__init__()\n", + "\n", + " # TODO: put file upload /viewer (with pandasdf or mercury, etc) in separate class (FileUploader)\n", + " self.file_uploader = FileUploader()\n", + "\n", + " # FileDownloader: ipyfilechooser and download button\n", + " self.file_downloader = FileDownloader()\n", + "\n", + "\n", + " self.file_upload = widgets.FileUpload(\n", + " accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n", + " multiple=False # True to accept multiple files upload else False\n", + " )\n", + " #self.file_browser = FileChooser()\n", + " #self.file_browser.sandbox_path = '.'\n", + " #self.file_browser.filter_pattern = ['*.pdf', '*.md']\n", + " #self.file_browser.title = 'File Selector' # Change the title (use '' to hide)\n", + "\n", + " # TODO: file_download\n", + " \n", + " \n", + " #self.vbox = VBox()\n", + " #self.vbox.children = (self.file_upload) # File viewer (hidden initially)\n", + "\n", + " #self.children = (self.vbox, )\n", + "\n", + " # File viewer (hidden in accordion)\n", + " self.selected_index = None # Hide the accordion content initially\n", + " self.titles = ('Course Files',) # this isn't working for some reason\n", + "\n", + " self.children = (self.file_uploader, self.file_downloader)" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "ae7ac138-aa5d-4c47-ae8d-18deb24b5627", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e61fd1312f36472ca756703398501e5a", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FileViewerView(children=(FileUploader(children=(FileUpload(value=(), accept='.pdf', description='Upload', mult…" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "file_viewer = FileViewerView()\n", + "file_viewer.title = ('Course Files', )\n", + "file_viewer" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "a05bb8a4-1a6c-4bc9-8875-efdc6a0f97af", + "metadata": {}, + "outputs": [], + "source": [ + "#uploader.value[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "id": "aa7f8081-d77d-4b6f-b739-31349382affe", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "class FileViewer(FileViewerView):\n", + " \n", + " def __init__(self, model):\n", + " super().__init__()\n", + " self.model = model\n", + " #self.file_browser.register_callback(self.observe_file_select)\n", + " \n", + " def observe_file_select(self, file_browser):\n", + " # update download button with correct filepath\n", + " # filepath stored in self.file_browser.selected\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "id": "23878cd3-a2d7-4316-b028-ac59e7fc2d55", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "dace147deb754399aad1745dafa3cfdb", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FileViewer(children=(FileUploader(children=(FileUpload(value=(), accept='.pdf', description='Upload', multiple…" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "file_model = FileModel()\n", + "fileviewer = FileViewer(file_model)\n", + "fileviewer" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "f4b9ae70-b76b-4fc7-8274-f9c274a3ebe3", + "metadata": {}, + "outputs": [], + "source": [ + " def file_upload(self, file_data):\n", + " \"\"\"\n", + " Handle file upload.\n", + " file_data: content of the file to be uploaded\n", + " \"\"\"\n", + " # Process file_data for upload\n", + " print(\"Uploading file...\")\n", + " # Here, you would include logic to handle the file upload\n", + "\n", + " def file_download(self, file_path):\n", + " \"\"\"\n", + " Handle file download.\n", + " file_path: path or identifier for the file to be downloaded\n", + " \"\"\"\n", + " # Process file download\n", + " print(\"Downloading file from:\", file_path)\n", + " # Here, you would include logic to handle the file download\n", + " # and perhaps return the file content or a success indicator" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "id": "7cd9b87a-4ce9-42b7-857d-037a7a0b3d44", + "metadata": {}, + "outputs": [], + "source": [ + "#| hide\n", + "import nbdev; nbdev.nbdev_export()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5a75be53-5c9c-43d2-9aaa-b4c3c2ecab8d", + "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.8.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}