Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HOWTO + FAQ on file management #3139

Open
freakboy3742 opened this issue Jan 29, 2025 · 0 comments
Open

Add HOWTO + FAQ on file management #3139

freakboy3742 opened this issue Jan 29, 2025 · 0 comments
Labels
documentation An improvement required in the project's documentation. enhancement New features, or improvements to existing features.

Comments

@freakboy3742
Copy link
Member

What is the problem or limitation you are having?

Discord and Discussions frequently include questions about how to access and store files in a running app. The advice is almost always the same - pointing at the app.paths documentation.

Describe the solution you'd like

We should add:

  1. A HOWTO document on managing files.
  2. A FAQ that points to the HOWTO.

Describe alternatives you've considered

n/a

Additional context

@rmartin16 recently provided a good model answer that could be used as the start of a draft. It uses a database as a use case.


This is a common idiom in BeeWare apps: shipping an app with a seeded database to be updated by the user after installation.

In this case, you include such a database (or csv file in this case) alongside your source code; storing this database in a resources directory would make sense.

Now, at runtime, this database is available at the App's installation file path. If you're using Toga, you can use the toga.paths.Paths API to access useful file system locations; for instance, the app's installation file path is available via toga.paths.Paths().app. More simply, this same path is available via your toga.App subclass at self.paths.app. Therefore, you can access your database via self.paths.app / "resources/db.csv" at runtime.

Since it is not safe to assume the app's installation location is writable, you should copy the seeded database to a known-writable location. For this, you should use self.paths.data as a safe and persistent location to store user data.

So, all put together, you add this to your app's startup() function...and youre good to go:

class HelloWorld(toga.App):
    def startup(self):
        self.db_filepath = self.paths.data / "app.db"
        if not self.db_filepath.exists():
            self.db_filepath.parent.mkdir(parents=True, exist_ok=True)
            shutil.copy(self.paths.app / f"resources/{self.db_filepath.name}", self.db_filepath)

@freakboy3742 freakboy3742 added documentation An improvement required in the project's documentation. enhancement New features, or improvements to existing features. labels Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation An improvement required in the project's documentation. enhancement New features, or improvements to existing features.
Projects
None yet
Development

No branches or pull requests

1 participant