A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
snippets: for cloud buckets (#3845)
Myles Scolnick44cbe6acb7e1
diff --git a/marimo/_snippets/data/aws-s3-0.py b/marimo/_snippets/data/aws-s3-0.pynew file mode 100644index 0000000000..eb0953d7ee--- /dev/null+++ b/marimo/_snippets/data/aws-s3-0.py@@ -0,0 +1,61 @@+# Copyright 2024 Marimo. All rights reserved.++import marimo++__generated_with = "0.11.6"+app = marimo.App()++[email protected](hide_code=True)+def _(mo):+ mo.md(r"""# Connect to AWS S3 Bucket""")+ return+++def _():+ import boto3+ import pandas as pd+ from botocore.exceptions import NoCredentialsError++ # Initialize S3 client+ s3_client = boto3.client(+ "s3",+ aws_access_key_id="YOUR_ACCESS_KEY",+ aws_secret_access_key="YOUR_SECRET_KEY",+ region_name="us-east-1", # Change to your region+ )++ # List buckets+ try:+ response = s3_client.list_buckets()+ buckets = [bucket["Name"] for bucket in response["Buckets"]]+ print("Available buckets:", buckets)+ except NoCredentialsError:+ print("Credentials not available")+ return NoCredentialsError, boto3, buckets, pd, response, s3_client+++def _(pd, s3_client):+ # Example: Read CSV file from S3+ bucket_name = "your-bucket-name"+ file_key = "path/to/your/file.csv"++ try:+ obj = s3_client.get_object(Bucket=bucket_name, Key=file_key)+ df = pd.read_csv(obj["Body"])+ df.head()+ except Exception as e:+ print(f"Error: {e}")+ return bucket_name, df, file_key, obj+++def _():+ import marimo as mo+ return (mo,)+++if __name__ == "__main__":+ app.run()diff --git a/marimo/_snippets/data/fsspec-0.py b/marimo/_snippets/data/fsspec-0.pynew file mode 100644index 0000000000..de6c232fc1--- /dev/null+++ b/marimo/_snippets/data/fsspec-0.py@@ -0,0 +1,59 @@+# Copyright 2024 Marimo. All rights reserved.++import marimo++__generated_with = "0.11.6"+app = marimo.App()++[email protected](hide_code=True)+def _(mo):+ mo.md(r"""# Using fsspec with Cloud Storage (S3 & GCS)""")+ return+++def _():+ import fsspec+ return (fsspec,)+++def _(fsspec):+ # Create filesystem objects+ s3 = fsspec.filesystem(+ "s3",+ key="YOUR_ACCESS_KEY", # AWS credentials+ secret="YOUR_SECRET_KEY",+ client_kwargs={"region_name": "us-east-1"},+ )++ # List buckets/files+ s3_files = s3.ls("your-bucket-name")+ print("S3 files:", s3_files[:5]) # Show first 5 files+ return s3, s3_files+++def _(fsspec):+ # Create filesystem objects+ gcs = fsspec.filesystem(+ "gcs",+ # GCS will use default credentials from environment+ token=None,+ )++ # List buckets/files+ gcs_files = gcs.ls("your-gcs-bucket")+ print("GCS files:", gcs_files[:5]) # Show first 5 files+ return gcs, gcs_files+++def _():+ import marimo as mo+ return (mo,)+++if __name__ == "__main__":+ app.run()diff --git a/marimo/_snippets/data/gcs-0.py b/marimo/_snippets/data/gcs-0.pynew file mode 100644index 0000000000..7f5912e51e--- /dev/null+++ b/marimo/_snippets/data/gcs-0.py@@ -0,0 +1,60 @@+# Copyright 2024 Marimo. All rights reserved.++import marimo++__generated_with = "0.11.6"+app = marimo.App()++[email protected](hide_code=True)+def _(mo):+ mo.md(r"""# Connect to Google Cloud Storage Bucket""")+ return+++def _():+ from google.cloud import storage+ import pandas as pd++ # Initialize GCS client+ # Note: This assumes you have set up authentication+ # (either through service account JSON or application default credentials)+ storage_client = storage.Client()++ # List buckets+ try:+ buckets = list(storage_client.list_buckets())+ print("Available buckets:", [bucket.name for bucket in buckets])+ except Exception as e:+ print(f"Error listing buckets: {e}")+ return buckets, pd, storage, storage_client+++def _(pd, storage_client):+ # Example: Read CSV file from GCS+ bucket_name = "your-bucket-name"+ blob_name = "path/to/your/file.csv"++ try:+ bucket = storage_client.bucket(bucket_name)+ blob = bucket.blob(blob_name)++ # Download to a string and create DataFrame+ content = blob.download_as_string()+ df = pd.read_csv(pd.io.common.BytesIO(content))+ df.head()+ except Exception as e:+ print(f"Error: {e}")+ return blob, blob_name, bucket, bucket_name, content, df+++def _():+ import marimo as mo+ return (mo,)+++if __name__ == "__main__":+ app.run()