Functions that draw images, write PDFs or run long
Available in self-service apps
Use the media function profile to draw images, write PDFs, run background jobs with progress, add libraries, and stay within the monthly media limits.
Use a media function when your app needs to produce a file, such as a chart, an image or a PDF, or do work that takes longer than an ordinary function allows. A function with the media profile can use numpy, Pillow, Skia, fpdf2 and HarfBuzz. It runs on its own heavier runner with one CPU and up to 1.5 GB of memory.
For ordinary functions, see Writing and testing functions.
Time allowed
| Where it runs | Time allowed |
|---|---|
| From an action | 20 seconds in all |
| In its tests | 15 seconds |
| In a job | 110 seconds |
Keep a file the function makes
The function writes the file under /tmp and calls ctx.files.put(name, path, content_type, record=...).
- Allowed types: PNG, JPEG, WebP, PDF, CSV and ZIP. Each file can be up to 200 MB, with 8 files in a run.
- With a record, the file becomes an attachment on that record, under that record's own rules. The record type needs attachments turned on.
- Without a record, the file is a short-lived link in the result. This suits previews.
The platform reads each file back and refuses one that is not what it says it is. When that happens, none of the run's writes happen.
Run longer work as a job
An action with output mode job answers at once and runs the function in the background for up to 110 seconds, as the person who started it. See Custom actions: buttons that run a function.
- The form waits and shows the result. Only the person who started the job can read it.
- About 60 jobs a person and 600 an app can start in an hour.
- In a job, call
ctx.progress(percent, message)to show how far the work has got on the form. - A Run function step on a media function goes through the same job queue with the same 110 seconds. Automations stay within 50 seconds. See Running a function when records change.
Use libraries and larger files
A media function can use packages, up to 50 MB in all.
A library from PyPI. Ask Rad Bot for it. Its card names the exact version and size for you to approve.
Your own code or data files. Upload a zip on the function's screen under Packages, then name the package in the function.
- The zip holds Python modules and data files only, nothing compiled. It can be at most 50 MB and 5,000 files.
- A name is 2 to 40 lowercase letters, digits,
-or_. A function names at most 8 packages. - Uploading is refused where the media profile is not switched on. The file counts against the app's storage.
- Through the API, an upload takes three calls: one with the name and size that returns an address, an upload of the zip to that address, and one that completes it by name so it is checked and kept. A fourth call lists the uploaded packages.
- The function imports a package's modules. Use
ctx.package_file(path)to find a data file.
A package's code runs without the checks a function's own code gets, so add only code you trust. It cannot reach anything outside the function's own run. Do not put personal data or secrets in an uploaded package: when you move it to live, it is copied through a public address that only its hash can find.
Fonts for PDFs
Noto fonts are at /opt/fonts, in Regular and Bold: NotoSans, NotoSerif, and NotoSans plus one of Devanagari, Bengali, Gujarati, Gurmukhi, Kannada, Malayalam, Tamil, Telugu, Arabic, Hebrew or Thai. Use pdf.add_font(name, fname=path) and pdf.set_text_shaping(True).
There are no Chinese, Japanese or Korean fonts.
Monthly limits
Each environment has 100 hours of media run time and 100 GB of file downloads a month. One end user can download 5 GB a day. During a free trial the limits are 2 hours and 5 GB.
Past a limit, media functions and downloads stop until the next month, with a message that says when. Nothing is charged. The app overview shows both figures. Your RADP contact can raise the limits.