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

UnicodeDecodeError when using accented characters in manifest.json #4713

Open
1 task done
nilton-medeiros opened this issue Jan 14, 2025 · 2 comments
Open
1 task done
Labels
status: awaiting response Further information is requested

Comments

@nilton-medeiros
Copy link

Duplicate Check

Describe the bug

I encountered a bug when running a Flet application that contains accented characters (Portuguese language) in the manifest.json file. The application throws a UnicodeDecodeError when trying to read the manifest file.

Environment:

  • Python: 3.12.7
  • Flet: 0.25.2
  • Operating System: Windows
  • Dependencies:
    • flet==0.25.2
    • flet-cli==0.25.2
    • flet-desktop==0.25.2
    • flet-web==0.25.2
    • uvicorn==0.34.0
    • websockets==14.1

The error occurs in:

File "...flet_web\patch_index.py", line 93, in patch_manifest_json
    manifest = json.loads(f.read())

Workaround:
Removing accented characters from the manifest.json file resolves the issue:

{
    "name": "Gestao de Estoque e Financeira",
    "short_name": "ESTOQUE RAPIDO",
    "start_url": ".",
    "display": "standalone",
    "background_color": "#000000",
    "theme_color": "#000000",
    "description": "Projeto desenvolvido pela Sistrom Sistemas Web!",
    "orientation": "natural"
}

Code sample

Code
import flet as ft

from src.pages.home import home
from src.pages.landing_page import landing_page
from src.pages.login import login


def main(page: ft.Page):
    # Força a limpeza do cache no início da aplicação
    page.clean()

    if not hasattr(page, 'sessions_data'):
        page.sessions_data = {}

    page.title = "EstoqueRápido"
    page.theme_mode = ft.ThemeMode.LIGHT
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.window.min_width = 300

    page.padding = 0
    page.spacing = 0

    # Rotas
    def route_change(e: ft.RouteChangeEvent):
        page.views.clear()
        pg_view = None

        match e.route:
            case '/':    # Raiz: Landing Page
                pg_view = ft.View(
                    route='/',
                    controls=[landing_page(page)],
                    appbar=page.appbar,
                    vertical_alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER,
                )
            case 'signup':  # Registro
                pg_view = ft.View(
                    route='/signup',
                    # controls=[signup(page)],
                    vertical_alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER,
                )
            case 'login':
                pg_view = ft.View(
                    route='/login',
                    controls=[login(page)],
                    vertical_alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER,
                )
            case 'logout':
                # page.sessions_data.clear()
                page.route = '/'
            case 'home':    # Homepage do usuário logado
                pg_view = ft.View(
                    route='/home',
                    controls=[home(page)],
                    vertical_alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER,
                )
            case _:
                # Opcional: tratamento para rotas não encontradas
                pg_view = ft.View(
                    route="/404",
                    controls=[
                        ft.AppBar(
                            title=ft.Text("Página não encontrada"),
                            bgcolor=ft.Colors.SURFACE_VARIANT
                        ),
                        ft.Text("404 - Página não encontrada", size=30),
                        ft.ElevatedButton(
                            text="Voltar para Início",
                            on_click=lambda _: page.go("/")
                        )
                    ],
                    vertical_alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER,
                )

        page.views.append(pg_view)
        page.update()

    def view_pop(e: ft.ViewPopEvent):
        page.views.pop()
        top_view = page.views[-1]
        page.go(top_view.route)

    page.on_route_change = route_change
    page.on_view_pop = view_pop
    page.go(page.route)


if __name__ == '__main__':
    ft.app(target=main, assets_dir="assets", view=ft.WEB_BROWSER)

To reproduce

  1. Create a manifest.json file with accented characters
  2. Run the application using flet run main.py -w

Original manifest.json (causes error):

{
    "name": "Gestão de Estoque e Financeira",
    "short_name": "ESTOQUE RÁPIDO",
    "start_url": ".",
    "display": "standalone",
    "background_color": "#000000",
    "theme_color": "#000000",
    "description": "Projeto desenvolvido pela Sistrom Sistemas Web!",
    "orientation": "natural"
}

### Expected behavior

The application should be able to handle UTF-8 encoded characters in the manifest.json file, especially considering that Flet is used internationally and many languages use accented characters.
Impact:
This bug affects applications that use non-ASCII characters in their manifest.json file, which is particularly important for internationalization and localization purposes.

### Screenshots / Videos

<details open>
<summary>Captures</summary>

[Upload media here]

</details>


### Operating System

Windows

### Operating system details

Windows 11 Pro

### Flet version

0.25.2

### Regression

I'm not sure / I don't know

### Suggestions

_No response_

### Logs

<details open><summary>Logs</summary>

```console
(venv) PS D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido> flet run main.py -w --verbose
D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\websockets\legacy\__init__.py:6: DeprecationWarning: websockets.legacy is deprecated; see https://websockets.readthedocs.io/en/stable/howto/upgrade.html for upgrade instructions
  warnings.warn(  # deprecated in 14.0 - 2024-11-09
D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\uvicorn\protocols\websockets\websockets_impl.py:17: DeprecationWarning: websockets.server.WebSocketServerProtocol is deprecated
  from websockets.server import WebSocketServerProtocol
http://127.0.0.1:51747
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 409, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 460, in handle
    await self.app(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 460, in handle
    await self.app(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet_web\fastapi\flet_static_files.py", line 78, in __call__
    await self.__once.do(self.__config, scope["root_path"])
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet\utils\once.py", line 13, in do
    await func(*args, **kwargs)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet_web\fastapi\flet_static_files.py", line 173, in __config
    patch_manifest_json(
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet_web\patch_index.py", line 93, in patch_manifest_json
    manifest = json.loads(f.read())
                          ^^^^^^^^
  File "C:\Users\nilto\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 78: character maps to <undefined>
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 409, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 460, in handle
    await self.app(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\starlette\routing.py", line 460, in handle
    await self.app(scope, receive, send)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet_web\fastapi\flet_static_files.py", line 78, in __call__
    await self.__once.do(self.__config, scope["root_path"])
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet\utils\once.py", line 13, in do
    await func(*args, **kwargs)
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet_web\fastapi\flet_static_files.py", line 173, in __config
    patch_manifest_json(
  File "D:\AppData\OneDrive\Desenvolvimento\Projetos\python\estoquerapido\venv\Lib\site-packages\flet_web\patch_index.py", line 93, in patch_manifest_json
    manifest = json.loads(f.read())
                          ^^^^^^^^
  File "C:\Users\nilto\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 78: character maps to <undefined>

Additional details

No response

@FeodorFitsner
Copy link
Contributor

Can you ensure manifest.json is saved in utf-8 encoding?

@ndonkoHenri ndonkoHenri added the status: awaiting response Further information is requested label Jan 14, 2025
@nilton-medeiros
Copy link
Author

Yes, I'm using VSCode in utf-8, to make sure, I used the commands:

import sys

print(sys.getdefaultencoding())

And the answer was utf-8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting response Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants