PageSourceSearch

https://apps-cookbook.dev/assets/js/1e93d0b5.66763309.js

js apps-cookbook.dev collected 2026-10-03 19:59:07 UTC 6,065 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkdocs=self.webpackChunkdocs||[]).push([[2134],{7233:(e,s,n)=>{n.r(s),n.d(s,{assets:()=>l,contentTitle:()=>t,default:()=>p,frontMatter:()=>a,metadata:()=>o,toc:()=>d});const o=JSON.parse('{"id":"reflex/volumes/volumes_download","title":"Download a file","description":"This recipe downloads a file from a Unity Catalog volume using the Databricks SDK for Python.","source":"@site/docs/reflex/volumes/volumes_download.mdx","sourceDirName":"reflex/volumes","slug":"/reflex/volumes/volumes_download","permalink":"/docs/reflex/volumes/volumes_download","draft":false,"unlisted":false,"editUrl":"https://github.com/databricks-solutions/databricks-apps-cookbook/edit/main/docs/docs/reflex/volumes/volumes_download.mdx","tags":[],"version":"current","sidebarPosition":2,"frontMatter":{"sidebar_position":2},"sidebar":"tutorialSidebar","previous":{"title":"Upload a file","permalink":"/docs/reflex/volumes/volumes_upload"},"next":{"title":"Invoke a model","permalink":"/docs/reflex/aiml/ml_serving_invoke"}}');var i=n(4848),r=n(8453);const a={sidebar_position:2},t="Download a file",l={},d=[{value:"Code snippet",id:"code-snippet",level:2},{value:"Resources",id:"resources",level:2},{value:"Permissions",id:"permissions",level:2},{value:"Dependencies",id:"dependencies",level:2}];function c(e){const s={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,r.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(s.header,{children:(0,i.jsx)(s.h1,{id:"download-a-file",children:"Download a file"})}),"\n",(0,i.jsxs)(s.p,{children:["This recipe downloads a file from a ",(0,i.jsx)(s.a,{href:"https://docs.databricks.com/en/volumes/index.html",children:"Unity Catalog volume"})," using the ",(0,i.jsx)(s.a,{href:"https://databricks-sdk-py.readthedocs.io/en/latest/",children:"Databricks SDK for Python"}),"."]}),"\n",(0,i.jsx)(s.admonition,{type:"note",children:(0,i.jsx)(s.p,{children:"Unlike notebooks, Databricks Apps does not support mounting Unity Catalog volumes and directly reading and writing files. As this code snippet demonstrates, each file needs to be downloaded to the app compute before being able to manipulate it."})}),"\n",(0,i.jsx)(s.h2,{id:"code-snippet",children:"Code snippet"}),"\n",(0,i.jsx)(s.pre,{children:(0,i.jsx)(s.code,{className:"language-python",metastring:'title="app.py"',children:'import reflex as rx\nimport os\nfrom databricks.sdk import WorkspaceClient\n\nw = WorkspaceClient()\n\nclass DownloadFileState(rx.State):\n    download_file_path: str = ""\n\n    async def handle_download(self):\n        response = w.files.download(self.download_file_path)\n        file_data = response.contents.read()\n        file_name = os.path.basename(self.download_file_path)\n\n        return rx.download(data=file_data, filename=file_name)\n'})}),"\n",(0,i.jsx)(s.h2,{id:"resources",children:"Resources"}),"\n",(0,i.jsxs)(s.ul,{children:["\n",(0,i.jsx)(s.li,{children:(0,i.jsx)(s.a,{href:"https://docs.databricks.com/aws/en/files/volumes",children:"Unity Catalog volume"})}),"\n"]}),"\n",(0,i.jsx)(s.h2,{id:"permissions",children:"Permissions"}),"\n",(0,i.jsxs)(s.p,{children:["Your ",(0,i.jsx)(s.a,{href:"https://docs.databricks.com/aws/en/dev-tools/databricks-apps/#how-does-databricks-apps-manage-authorization",children:"app service principal"})," needs the following permissions:"]}),"\n",(0,i.jsxs)(s.ul,{children:["\n",(0,i.jsxs)(s.li,{children:[(0,i.jsx)(s.code,{children:"USE CATALOG"})," on the volume's catalog"]}),"\n",(0,i.jsxs)(s.li,{children:[(0,i.jsx)(s.code,{children:"USE SCHEMA"})," on the volume's schema"]}),"\n",(0,i.jsxs)(s.li,{children:[(0,i.jsx)(s.code,{children:"READ VOLUME"})," on the volume"]}),"\n"]}),"\n",(0,i.jsxs)(s.p,{children:["See ",(0,i.jsx)(s.a,{href:"https://docs.databricks.com/en/volumes/privileges.html#privileges-required-for-volume-operations",children:"Privileges required for volume operations"})," for more information."]}),"\n",(0,i.jsxs)(s.p,{children:["If you declare volume access in a Databricks Asset Bundle, ",(0,i.jsx)(s.code,{children:"resources.apps[*].resources[*].uc_securable"}
1)," may not grant ",(0,i.jsx)(s.code,{children:"USE_CATALOG"})," and ",(0,i.jsx)(s.code,{children:"USE_SCHEMA"})," on the parent catalog and schema (the app still needs them at runtime). As a temporary workaround until bundles can declare those parent grants, add the privileges manually, or see ",(0,i.jsx)(s.a,{href:"https://github.com/salihbout/apps_grants_sync",children:"apps_grants_sync"}),": an example Databricks App and Asset Bundle that wires ",(0,i.jsx)(s.code,{children:"experimental.scripts.postdeploy"})," so parent privileges are applied after each ",(0,i.jsx)(s.code,{children:"databricks bundle deploy"})," (copy its ",(0,i.jsx)(s.code,{children:"tools/"})," into your bundle or mirror the same pattern in ",(0,i.jsx)(s.code,{children:"databricks.yml"}),")."]}),"\n",(0,i.jsx)(s.h2,{id:"dependencies",children:"Dependencies"}),"\n",(0,i.jsxs)(s.ul,{children:["\n",(0,i.jsxs)(s.li,{children:[(0,i.jsx)(s.a,{href:"https://pypi.org/project/databricks-sdk/",children:"Databricks SDK for Python"})," - ",(0,i.jsx)(s.code,{children:"databricks-sdk"})]}),"\n",(0,i.jsxs)(s.li,{children:[(0,i.jsx)(s.a,{href:"https://pypi.org/project/reflex/",children:"Reflex"})," - ",(0,i.jsx)(s.code,{children:"reflex"})]}),"\n"]}),"\n",(0,i.jsx)(s.pre,{children:(0,i.jsx)(s.code,{className:"language-python",metastring:'title="requirements.txt"',children:"databricks-sdk\nreflex\n"})})]})}function p(e={}){const{wrapper:s}={...(0,r.R)(),...e.components};return s?(0,i.jsx)(s,{...e,children:(0,i.jsx)(c,{...e})}):c(e)}},8453:(e,s,n)=>{n.d(s,{R:()=>a,x:()=>t});var o=n(6540);const i={},r=o.createContext(i);function a(e){const s=o.useContext(r);return o.useMemo((function(){return"function"==typeof e?e(s):{...s,...e}}),[s,e])}function t(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(e.components),o.createElement(r.Provider,{value:s},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.