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

Access the assets dir easily #4648

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

omamkaz
Copy link
Contributor

@omamkaz omamkaz commented Jan 2, 2025

  • Added the assets_dir_path method to be available for a developer to work with assets

Description

Fixes #3083

Test Code

import base64

import flet as ft


def lottie_base64(name: str) -> str:
    with open(ft.assets_dir_path("animations", name), "rb") as fr:
        return base64.b64encode(fr.read()).decode()


def main(page: ft.Page) -> None:
    page.title = "Test assets_dir_path"
    page.window.width = page.window.height = 600

    page.add(
        ft.Lottie(src_base64=lottie_base64("no_internet.json"), fit=ft.ImageFit.COVER)
    )

if __name__ == "__main__":
    ft.app(main)

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist

  • I signed the CLA.
  • My code follows the style guidelines of this project

Screenshots

Additional details

Tested on

[x] Linux
[] Windows
[] MacOS

Summary by Sourcery

New Features:

  • Introduce the assets_dir_path method in the flet.app module to provide convenient access to the assets directory.

- Added the `assets_dir_path` method to be available for a developer to work with `assets`
@FeodorFitsner
Copy link
Contributor

Thanks for the your PR. Could you give an example of how it's supposed to be used in a user app?

@omamkaz
Copy link
Contributor Author

omamkaz commented Jan 8, 2025

Thanks for the your PR. Could you give an example of how it's supposed to be used in a user app?

In my case the Lottie animation files don't work just base64, so I want to open the file and pass it as base64 data, and other examples of data you want to work with it like .csv files.

Example

class _LottieFiles:
    def __init__(self):
        for name in os.listdir(ft.assets_dir_path("animations")):
            # self.__setattr__(name.removesuffix(".json"), self.lottie_file(name))
            self.__setattr__(name.removesuffix(".json"), self.lottie_base64(name))

    @classmethod
    def lottie_file(cls, name: str) -> str:
        return ft.assets_dir_path("animations", name)

    @classmethod
    def lottie_base64(cls, name: str) -> str:
        with open(ft.assets_dir_path("animations", name), "rb") as fr:
            return base64.b64encode(fr.read()).decode()

LottieFiles = _LottieFiles()

## Test
ft.Lottie(src_base64=LottieFiles.no_internet., fit=ft.ImageFit.COVER)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Local Lottie Assets Not Rendering in Deployed Android APK
2 participants