File Explorer Dashboard¶
The File Explorer Dashboard provides an interactive web interface for browsing, managing, and analyzing files from both local file systems and Google Cloud Storage. It offers a comprehensive file management solution with visualization capabilities.
Features¶
Multi-source Support: Browse files from local directories and Google Cloud Storage
File Type Support: Handle various file formats including images, markdown, JSON, and HTML
Interactive Tree View: Navigate through directory structures with an intuitive tree interface
File Preview: Preview content of supported file types directly in the browser
Configuration Management: Save and load dashboard configurations
Responsive Design: Optimized for different screen sizes
Main Components¶
File Explorer App¶
- class mango_dashboard.file_explorer.dashboards.file_explorer_app.FileExplorerApp(path: str = None, editable: bool = True, config_path: str = None, file_handler: FileExplorerHandler = None)¶
Bases:
object
A Streamlit-based file explorer application.
This class provides a web-based interface for exploring and managing files and directories. It supports various file formats including CSV, Excel, JSON, images, HTML plots, and Markdown files. The application can work with both local file systems and Google Cloud Storage.
- Features:
Interactive file browsing with tree view
File content preview and editing
Support for multiple file formats
Configurable layout and display options
Integration with GCP storage
- run()¶
Run the complete Streamlit file explorer application.
Main entry point that orchestrates the rendering of all application components in the correct order: header, configuration sidebar, folder tree, and main content area. Handles error cases gracefully with appropriate user feedback.
- Returns:
None
- Return type:
None
File Explorer Handlers¶
- class mango_dashboard.file_explorer.dashboards.file_explorer_handlers.FileExplorerHandler(path: str)¶
Bases:
ABC
Abstract base class for file exploration handlers.
This class defines the interface that all file handlers must implement. It provides abstract methods for common file operations including directory checking, path validation, file reading, and JSON writing.
- Attributes:
_path (str): The base path for file operations.
- abstract is_dir(path: str)¶
Check if the given path is a directory.
- Parameters:
path (str) – The path to check
- Returns:
True if the path is a directory, False otherwise
- Return type:
bool
- abstract path_exists(path: str)¶
Check if the given path exists.
- Parameters:
path (str) – The path to check
- Returns:
True if the path exists, False otherwise
- Return type:
bool
- abstract get_file_or_folder_paths(path: str, element_type: str) List[str] ¶
Get a list of file or folder paths within the specified directory.
- Parameters:
path (str) – The directory path to search in
element_type (str) – Type of elements to return (‘file’ or ‘folder’)
- Returns:
List of file or folder paths
- Return type:
List[str]
- Raises:
ValueError – If element_type is not ‘file’ or ‘folder’
- abstract read_img(path: str)¶
Read an image file from the specified path.
- Parameters:
path (str) – The path to the image file
- Returns:
The loaded image object
- Return type:
PIL.Image.Image
- abstract read_markdown(path: str)¶
Read a markdown file from the specified path.
- Parameters:
path (str) – The path to the markdown file
- Returns:
The content of the markdown file
- Return type:
str
- abstract read_json(path: str)¶
Read a JSON file from the specified path.
- Parameters:
path (str) – The path to the JSON file
- Returns:
The parsed JSON data
- Return type:
dict or list
- abstract write_json_fe(path: str, data: dict)¶
Write data to a JSON file at the specified path.
- Parameters:
path (str) – The path where to write the JSON file
data (dict) – The data to write to the JSON file
- abstract read_html(path: str, encoding: str = 'utf-8')¶
Read an HTML file from the specified path.
- Parameters:
path (str) – The path to the HTML file
encoding (str) – The encoding to use
- Returns:
The content of the HTML file
- Return type:
str
Local File System Handler¶
- class mango_dashboard.file_explorer.dashboards.file_explorer_handlers.LocalFileExplorerHandler(path: str)¶
Bases:
FileExplorerHandler
File explorer handler for local filesystem operations.
This class implements the FileExplorerHandler interface for local filesystem operations using standard Python libraries like os and pathlib.
- is_dir(path: str)¶
Check if the given path is a directory using os.path.isdir.
- Parameters:
path (str) – The path to check
- Returns:
True if the path is a directory, False otherwise
- Return type:
bool
- path_exists(path: str)¶
Check if the given path exists using os.path.exists.
- Parameters:
path (str) – The path to check
- Returns:
True if the path exists, False otherwise
- Return type:
bool
- get_file_or_folder_paths(path: str, element_type: str)¶
Get a list of file or folder paths within the specified directory.
Uses os.walk to recursively traverse the directory and collect paths based on the specified element type.
- Parameters:
path (str) – The directory path to search in
element_type (str) – Type of elements to return (‘file’ or ‘folder’)
- Returns:
List of file or folder paths
- Return type:
List[str]
- Raises:
ValueError – If element_type is not ‘file’ or ‘folder’
- read_img(path: str)¶
Read an image file using PIL.Image.open.
- Parameters:
path (str) – The path to the image file
- Returns:
The loaded image object
- Return type:
PIL.Image.Image
- read_markdown(path: str)¶
Read a markdown file using pathlib.Path.read_text.
- Parameters:
path (str) – The path to the markdown file
- Returns:
The content of the markdown file
- Return type:
str
- read_json(path: str)¶
Read a JSON file using json.load.
- Parameters:
path (str) – The path to the JSON file
- Returns:
The parsed JSON data
- Return type:
dict or list
- write_json_fe(path: str, data: dict | list)¶
Write data to a JSON file using mango.processing.write_json.
- Parameters:
path (str) – The path where to write the JSON file
data (Union[dict, list]) – The data to write to the JSON file
- read_html(path: str, encoding: str = 'utf-8')¶
Read an HTML file with specified encoding.
- Parameters:
path (str) – The path to the HTML file
encoding (str) – The encoding to use
- Returns:
The content of the HTML file
- Return type:
str
Google Cloud Storage Handler¶
- class mango_dashboard.file_explorer.dashboards.file_explorer_handlers.GCPFileExplorerHandler(path: str, gcp_credentials_path: str)¶
Bases:
FileExplorerHandler
File explorer handler for Google Cloud Storage operations.
This class implements the FileExplorerHandler interface for Google Cloud Storage operations using the google-cloud-storage library.
- Attributes:
_gcp_client (storage.Client): The Google Cloud Storage client. _bucket_name (str): The name of the GCS bucket. _bucket (storage.bucket.Bucket): The bucket object for operations.
- is_dir(path: str)¶
Check if the given path is a directory in GCS.
This method is not fully implemented for GCS as directories are virtual constructs in cloud storage.
- Parameters:
path (str) – The path to check
- Returns:
Always returns False as this is not implemented
- Return type:
bool
- path_exists(path: str)¶
Check if the given path exists in GCS.
Lists blobs with the specified prefix and checks if any match the given path.
- Parameters:
path (str) – The path to check
- Returns:
True if the path exists, False otherwise
- Return type:
bool
- get_file_or_folder_paths(path: str, element_type: str = 'file')¶
Get a list of file or folder paths within the specified GCS directory.
Lists blobs in the bucket with the specified prefix and categorizes them as files or folders based on whether they end with ‘/’.
- Parameters:
path (str) – The directory path to search in
element_type (str) – Type of elements to return
- Returns:
List of file or folder paths with full GCS URLs
- Return type:
List[str]
- read_img(path: str)¶
Read an image file from GCS using PIL.Image.open.
Downloads the blob as bytes and opens it as an image.
- Parameters:
path (str) – The GCS path to the image file
- Returns:
The loaded image object
- Return type:
PIL.Image.Image
- read_markdown(path: str)¶
Read a markdown file from GCS.
Downloads the blob as a string and decodes it as UTF-8.
- Parameters:
path (str) – The GCS path to the markdown file
- Returns:
The content of the markdown file
- Return type:
str
- read_json(path: str)¶
Read a JSON file from GCS.
Downloads the blob as a string, decodes it, and parses as JSON.
- Parameters:
path (str) – The GCS path to the JSON file
- Returns:
The parsed JSON data
- Return type:
dict or list
- write_json_fe(path: str, data: dict | list)¶
Write data to a JSON file in GCS.
Converts the data to JSON string and uploads it to the specified path.
- Parameters:
path (str) – The GCS path where to write the JSON file
data (Union[dict, list]) – The data to write to the JSON file
- read_html(path: str, encoding: str = 'utf-8')¶
Read an HTML file from GCS.
Downloads the blob as a string and decodes it with the specified encoding.
- Parameters:
path (str) – The GCS path to the HTML file
encoding (str) – The encoding to use
- Returns:
The content of the HTML file
- Return type:
str
Usage¶
The File Explorer Dashboard can be launched using Streamlit:
streamlit run mango_dashboard/file_explorer/dashboards/file_explorer_app.py