{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "in_dir = os.path.join(os.path.expanduser('~'), 'Documents')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tree = geemap.file_browser(in_dir, show_hidden=False)\n", "tree" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def file_browser(in_dir=None, show_hidden=False):\n", " \"\"\"Creates a simple file browser and text editor.\n", "\n", " Args:\n", " in_dir (str, optional): The input directory. Defaults to None, which will use the current working directory.\n", " show_hidden (bool, optional): Whether to show hidden files/folders. Defaults to False.\n", "\n", " Returns:\n", " object: An ipywidget.\n", " \"\"\"\n", " import os\n", " import ipywidgets as widgets\n", " from ipytree import Tree, Node\n", " \n", " if in_dir is None:\n", " in_dir = os.getcwd()\n", "\n", " if not os.path.exists(in_dir):\n", " print('The provided directory does not exist.')\n", " return\n", " elif not os.path.isdir(in_dir):\n", " print('The provided path is not a valid directory.')\n", " return\n", "\n", " if in_dir.endswith('/'):\n", " in_dir = in_dir[:-1]\n", "\n", " full_widget = widgets.HBox()\n", " left_widget = widgets.VBox()\n", "\n", " right_widget = widgets.VBox()\n", "\n", " path_widget = widgets.Text()\n", " path_widget.layout.min_width = '475px'\n", " save_widget = widgets.Button(\n", " description='Save', button_style='primary', tooltip='Save edits to file.')\n", " info_widget = widgets.HBox()\n", " info_widget.children = [path_widget, save_widget]\n", "\n", " text_widget = widgets.Textarea()\n", " text_widget.layout.width = '630px'\n", " text_widget.layout.height = '600px'\n", "\n", " right_widget.children = [info_widget, text_widget]\n", " full_widget.children = [left_widget]\n", "\n", " search_box = widgets.Text(placeholder='Search files/folders...')\n", " search_box.layout.width = '310px'\n", " tree_widget = widgets.Output()\n", " tree_widget.layout.max_width = '310px'\n", " tree_widget.overflow = 'auto'\n", "\n", " left_widget.children = [search_box, tree_widget]\n", "\n", " tree = Tree(multiple_selection=False)\n", " tree_dict = {}\n", "\n", " def on_button_clicked(b):\n", " content = text_widget.value\n", " out_file = path_widget.value\n", "\n", " out_dir = os.path.dirname(out_file)\n", " if not os.path.exists(out_dir):\n", " os.makedirs(out_dir)\n", "\n", " with open(out_file, 'w') as f:\n", " f.write(content)\n", "\n", " text_widget.disabled = True\n", " text_widget.value = 'The content has been saved successfully.'\n", " save_widget.disabled = True\n", " path_widget.disabled = True\n", "\n", " if (out_file not in tree_dict.keys()) and (out_dir in tree_dict.keys()):\n", " node = Node(os.path.basename(out_file))\n", " tree_dict[out_file] = node\n", " parent_node = tree_dict[out_dir]\n", " parent_node.add_node(node)\n", "\n", " save_widget.on_click(on_button_clicked)\n", "\n", " def search_box_callback(text):\n", "\n", " with tree_widget:\n", " if text.value == '':\n", " print('Loading...')\n", " tree_widget.clear_output(wait=True)\n", " display(tree)\n", " else:\n", " tree_widget.clear_output()\n", " print('Searching...')\n", " tree_widget.clear_output(wait=True)\n", " sub_tree = search_api_tree(text.value, tree_dict)\n", " display(sub_tree)\n", " search_box.on_submit(search_box_callback)\n", "\n", " def handle_file_click(event):\n", " if event['new']:\n", " cur_node = event['owner']\n", " for key in tree_dict.keys():\n", " if (cur_node is tree_dict[key]) and (os.path.isfile(key)):\n", " try:\n", " with open(key) as f:\n", " content = f.read()\n", " text_widget.value = content\n", " text_widget.disabled = False\n", " path_widget.value = key\n", " path_widget.disabled = False\n", " save_widget.disabled = False\n", " full_widget.children = [left_widget, right_widget]\n", " except Exception as e:\n", " path_widget.value = key\n", " path_widget.disabled = True\n", " save_widget.disabled = True\n", " text_widget.disabled = True\n", " text_widget.value = 'Failed to open {}.'.format(\n", " cur_node.name) + '\\n\\n' + str(e)\n", " return\n", "\n", " def handle_folder_click(event):\n", " if event['new']:\n", " full_widget.children = [left_widget]\n", " text_widget.value = ''\n", "\n", " root_name = in_dir.split('/')[-1]\n", " root_node = Node(root_name)\n", " tree_dict[in_dir] = root_node\n", " tree.add_node(root_node)\n", " root_node.observe(handle_folder_click, 'selected')\n", "\n", " for root, d_names, f_names in os.walk(in_dir):\n", "\n", " if not show_hidden:\n", " folders = root.split('/')\n", " for folder in folders:\n", " if folder.startswith('.'):\n", " continue\n", " for d_name in d_names:\n", " if d_name.startswith('.'):\n", " d_names.remove(d_name)\n", " for f_name in f_names:\n", " if f_name.startswith('.'):\n", " f_names.remove(f_name)\n", "\n", " d_names.sort()\n", " f_names.sort()\n", "\n", " if root not in tree_dict.keys():\n", " name = root.split('/')[-1]\n", " dir_name = os.path.dirname(root)\n", " parent_node = tree_dict[dir_name]\n", " node = Node(name)\n", " tree_dict[root] = node\n", " parent_node.add_node(node)\n", " node.observe(handle_folder_click, 'selected')\n", "\n", " if len(f_names) > 0:\n", " parent_node = tree_dict[root]\n", " parent_node.opened = False\n", " for f_name in f_names:\n", " node = Node(f_name)\n", " node.icon = 'file'\n", " full_path = os.path.join(root, f_name)\n", " tree_dict[full_path] = node\n", " parent_node.add_node(node)\n", " node.observe(handle_file_click, 'selected')\n", "\n", " with tree_widget:\n", " tree_widget.clear_output()\n", " display(tree)\n", "\n", " return full_widget" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tree = file_browser()\n", "tree" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }