Files
lancedb/docs/web/snippets/photo_tutorial.mdx
T
Xuanwo 9f58d8aab6 docs: add an ordinary Blob v2 photo tutorial (#5087)
The quickstart links to a Blob example that assumes both tables already
exist. Replace that gap with a from-empty photo tutorial: generate real
PNG files with the standard library, ingest an ordinary Blob v2 table,
filter metadata, save and compare exact image bytes, then create and
verify a filtered destination through Enterprise SQL or an Embedded
materialized copy.

Keep Early access and distinguish Enterprise 1.12.0 row-ID fetches from
deployment-dependent snapshot-bound reads. Cover source stability, range
reads and Function readers for large media, ambiguous write recovery,
and the absence of a SQL constructor for uploading client-side files.

The executable Embedded workflow passed on Python SDK 0.42.0b17 using
disposable local storage. Generated snippets, MDX compilation,
combined-site `mint validate`, and `mint broken-links --check-anchors
--check-redirects` passed. Enterprise connection/SQL examples were
checked against the public API and existing 1.12 documentation but were
not executed against an Enterprise deployment; this does not establish
SQL copy behavior after source cleanup. No saved editions or publication
state change.

Fixes
[OSS-3102](https://linear.app/lancedb/issue/OSS-3102/walk-users-from-image-files-to-a-filtered-blob-v2-table-copy).
2026-11-18 17:19:02 +08:00

22 lines
3.8 KiB
Plaintext

{/* Auto-generated by scripts/mdx_snippets_gen.py. Do not edit manually. */}
export const PyPhotoBytes = "rows = (\n photos.search()\n .where(\"label = 'red'\")\n .select([\"id\", \"image\"])\n .limit(10)\n .to_list(blob_mode=\"bytes\")\n)\nassert rows == [{\"id\": 1, \"image\": (inputs / \"red.png\").read_bytes()}]\n";
export const PyPhotoConnectEmbedded = "import lancedb\n\ndb = lancedb.connect(\"./photo-db\")\n";
export const PyPhotoConnectEnterprise = "import os\n\nimport lancedb\n\ndb = lancedb.connect(\n os.environ[\"LANCEDB_URI\"],\n api_key=os.environ[\"LANCEDB_API_KEY\"],\n region=os.environ[\"LANCEDB_REGION\"],\n host_override=os.environ[\"LANCEDB_HOST_OVERRIDE\"],\n sql_host_override=os.environ[\"LANCEDB_SQL_HOST_OVERRIDE\"],\n)\n";
export const PyPhotoCopyEmbedded = "selected = (\n photos.search()\n .where(\"label = 'red'\")\n .select([\"id\", \"label\", \"image\"])\n .limit(10)\n .to_arrow(blob_mode=\"bytes\")\n)\ndb.create_table(\"photo_subset\", data=selected, schema=schema)\n";
export const PyPhotoCopySql = "db.execute_query(\n \"CREATE TABLE photo_subset (id BIGINT, label VARCHAR, image BLOB)\"\n).read_all()\ndb.execute_query(\n \"INSERT INTO photo_subset (id, label, image) \"\n \"SELECT id, label, image FROM photos WHERE label = 'red'\"\n).read_all()\n";
export const PyPhotoFetch = "hits = (\n photos.search()\n .where(\"label = 'red'\")\n .select([\"id\", \"label\"])\n .with_row_id(True)\n .limit(10)\n .to_list()\n)\nassert [row[\"id\"] for row in hits] == [1]\npayloads = photos.fetch_blobs(\"image\", [row[\"_rowid\"] for row in hits]).to_pylist()\nassert payloads == [(inputs / \"red.png\").read_bytes()]\nsaved = Path(\"selected-red.png\")\nsaved.write_bytes(payloads[0])\nassert saved.read_bytes() == (inputs / \"red.png\").read_bytes()\n";
export const PyPhotoFiles = "import struct\nimport zlib\nfrom pathlib import Path\n\ndef solid_png(rgb):\n def chunk(kind, data):\n checksum = zlib.crc32(kind + data) & 0xFFFFFFFF\n return (\n struct.pack(\">I\", len(data)) + kind + data + struct.pack(\">I\", checksum)\n )\n\n header = struct.pack(\">IIBBBBB\", 8, 8, 8, 2, 0, 0, 0)\n pixels = (b\"\\x00\" + bytes(rgb) * 8) * 8\n return (\n b\"\\x89PNG\\r\\n\\x1a\\n\"\n + chunk(b\"IHDR\", header)\n + chunk(b\"IDAT\", zlib.compress(pixels))\n + chunk(b\"IEND\", b\"\")\n )\n\ninputs = Path(\"photo-inputs\")\ninputs.mkdir()\nfor label, rgb in [(\"red\", (255, 0, 0)), (\"blue\", (0, 0, 255))]:\n (inputs / f\"{label}.png\").write_bytes(solid_png(rgb))\n";
export const PyPhotoIngest = "import pyarrow as pa\n\nschema = pa.schema(\n [\n pa.field(\"id\", pa.int64(), nullable=False),\n pa.field(\"label\", pa.string(), nullable=False),\n lancedb.blob(\"image\", nullable=False),\n ],\n metadata={\"lance.blob.managed\": \"false\"},\n)\nphotos = db.create_table(\"photos\", schema=schema)\nphotos.add(\n [\n {\"id\": 1, \"label\": \"red\", \"image\": (inputs / \"red.png\").read_bytes()},\n {\"id\": 2, \"label\": \"blue\", \"image\": (inputs / \"blue.png\").read_bytes()},\n ]\n)\nassert photos.count_rows() == 2\n";
export const PyPhotoRanges = "with photos.fetch_blob_files(\"image\", [hits[0][\"_rowid\"]])[0] as reader:\n assert reader.read_range(0, 8) == b\"\\x89PNG\\r\\n\\x1a\\n\"\n";
export const PyPhotoVerify = "photo_subset = db.open_table(\"photo_subset\")\nassert photo_subset.count_rows() == 1\ncopied = (\n photo_subset.search()\n .select([\"id\", \"label\"])\n .with_row_id(True)\n .limit(10)\n .to_list()\n)\nassert [(row[\"id\"], row[\"label\"]) for row in copied] == [(1, \"red\")]\ncopied_bytes = photo_subset.fetch_blobs(\n \"image\", [row[\"_rowid\"] for row in copied]\n).to_pylist()\nassert copied_bytes == [(inputs / \"red.png\").read_bytes()]\n";