Skip to content

Access the assets dir easily #4648

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

osamalzabidi
Copy link
Contributor

@osamalzabidi osamalzabidi commented Jan 2, 2025

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

Description

Fixes flet-dev/flet-lottie#12

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?

@osamalzabidi
Copy link
Contributor Author

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)

@FeodorFitsner
Copy link
Contributor

Thank you for your PR! At this time, we’re not accepting refactoring PRs, non-trivial changes, or new features for Flet v0.2x. Our focus is currently on preparing the upcoming “v1” release (in the v1 branch). However, bug fixes for Flet 0.2x are still welcome.

@osamalzabidi osamalzabidi deleted the patch-4 branch June 14, 2025 00:31
# for free to join this conversation on GitHub. Already have an account? # 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