Skip to main content

Files

The abstra.files module provides functions to interact with the Files directory. These functions allow you to access persistent storage, create public links, and manage files effectively.

info

For easier testing, when using the local editor, the Files directory is located under .abstra/persistent. All functions listed here will read and write to this folder, which is not synced with the cloud storage. Keep in mind that files created locally will not be available in the cloud environment.


get_persistent_dir()

Returns the path of the Files directory, where persistent files can be stored and accessed. Use this function to get a persistent directory where files remain stored between executions and deploys.

Returns

TypeDescription
pathlib.PathPath to the persistent Files directory.

Example

from abstra.files import get_persistent_dir

# Get the persistent directory path
persistent_dir = get_persistent_dir()

# Create and write to a file in the persistent directory
logs = persistent_dir / 'logs.txt'
logs.write_text('Hello World')

create_public_link(file_path)

This function copies the file to _public and generates a shareable link.

info

As mentioned above, create_public_link won't generate URL accessible over the internet when using in the local editor

caution

Files placed in _public can be accessed from the web by anyone with the link.

Parameters

ParameterTypeDescription
file_pathstr | pathlib.PathPath to the file that should be made public.

Returns

TypeDescription
strURL to the publicly available file.

Example 1: Simple Usage

import abstra.files as files

# Create a test file
file_path = files.get_persistent_dir() / "test-file.txt"
file_path.write_text("This is a test file.")

# Generate a public link for the file
public_link = files.create_public_link(file_path)
print("Public Link:", public_link)

import abstra.forms as forms
import abstra.files as files
import abstra.messages as messages

# Get user input
user = forms.get_user()
date = forms.read_date("Report date")
isodate = date.isoformat()

# Generate report and save it in the persistent directory
report_path = files.get_persistent_dir() / f"{isodate}-report.txt"
report_path.write_text(f"{user.email}: {isodate}")

# Create a public link for the report
report_link = files.create_public_link(report_path)

# Send the report link via email
title = f"Report from {date}"
message = f"Hi {user.email}, here is your report: {report_link}."
messages.send_email(to=user.email, message=message, title=title)

# Display the report link on the UI
forms.Page().display("Report also sent via email").display_link(report_link, link_text="View Report").run(end_program=True)

get_public_dir()

Returns the path of the _public directory inside Files. Files placed in this directory are publicly accessible at your-project.abstra.app/_public.

caution

Files placed in _public can be accessed from the web by anyone with the link.

Returns

TypeDescription
pathlib.PathPath to the _public directory.

Example

from abstra.files import get_public_dir

# Get the public directory path
public = get_public_dir()

# Create a simple text file that is publicly accessible
public_file = public / "public-note.txt"
public_file.write_text("This file is accessible to everyone.")

print(f"Public file created at: {public_file}")