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

ASGI exception get raised when calling pc.upload_files() #794

Closed
ramsy0dev opened this issue Apr 9, 2023 · 1 comment
Closed

ASGI exception get raised when calling pc.upload_files() #794

ramsy0dev opened this issue Apr 9, 2023 · 1 comment

Comments

@ramsy0dev
Copy link

I'm trying to use the pc.upload() and when add a pc.buttton() that on_click will call an other function that takes in pc.upload_files() but when selecting a file and clicking on upload, i get the following exception:

──────────── Starting Pynecone App ─────────────
───────── Installing frontend packages ─────────
bun install v0.5.5 (242dcea2)
───────────────── App Running ──────────────────
$ next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
event - compiled client and server successfully in 1041 ms (757 modules)
wait  - compiling...
event - compiled client and server successfully in 144 ms (757 modules)
wait  - compiling /404 (client and server)...
event - compiled client and server successfully in 123 ms (761 modules)
warn  - Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/basic-features/fast-refresh#how-it-works
wait  - compiling / (client and server)...
event - compiled client and server successfully in 264 ms (904 modules)
warn  - Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/basic-features/fast-refresh#how-it-works
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/ramsy/.local/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/home/ramsy/.local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 92, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 147, in simple_response
    await self.app(scope, receive, send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/home/ramsy/.local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/home/ramsy/.local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/routing.py", line 706, in __call__
    await route.handle(scope, receive, send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/home/ramsy/.local/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/home/ramsy/.local/lib/python3.10/site-packages/fastapi/routing.py", line 235, in app
    raw_response = await run_endpoint_function(
  File "/home/ramsy/.local/lib/python3.10/site-packages/fastapi/routing.py", line 161, in run_endpoint_function
    return await dependant.call(**values)
  File "/home/ramsy/.local/lib/python3.10/site-packages/pynecone/app.py", line 521, in upload_file
    or types._issubclass(v, UploadFile)
  File "/home/ramsy/.local/lib/python3.10/site-packages/pynecone/utils/types.py", line 113, in _issubclass
    return cls_check_base == Any or issubclass(cls_base, cls_check_base)
TypeError: issubclass() arg 1 must be a class

To Reproduce
This is my current implementation of pc.upload():

import pynecone as pc

from MusBleep.state import MusBleepState

def dragOrDropUpload():
    """
        Drag or drop to upload form
    """
    return pc.vstack(
        pc.upload(
            pc.vstack(
                pc.button(
                    "Select File",
                    color="rgb(107,99,246)",
                    bg="white",
                    border=f"1px solid rgb(107,99,246)",
                ),
                pc.text(
                    "Drag and drop music file here or click to select a music file"
                ),
            ),
                border=f"1px dotted rgb(107,99,246)",
                padding="5em",
            ),
            pc.button(
                "Upload",
                on_click=lambda: MusBleepState.handle_music_upload(
                    pc.upload_files()
                ),
            ),
        )

and this is my app state class:

import requests
import pynecone as pc

class MusBleepState(pc.State):
    """
        Main state handler for the web app
    """
    is_darkMode = True

    def change_mode(self) -> None:
        """
            Changes the mode
        """
        self.is_darkMode = not self.is_darkMode
    
    # Music Upload
    bleeped_music_url: str = ""
    music_name: str = "Nothing to show"  # default value
    is_upload_finish: bool = False
    is_music_upload_exception_raised: bool = False # Used to show a warning when the music upload fails
    is_faild_to_bleep_music_exception_raised: bool = False

    # Play Music states
    is_playing: bool = False

    async def handle_music_upload(self, file: pc.UploadFile) -> None:
        """
            Upload the music to the API 
            to get bleeped
        """
        API_URL = "http://127.0.0.1:9090"

        MUSIC_CONTENT = await file.read()
        MUSIC_NAME = file.filename

        print(MUSIC_NAME)
        FILE = {
            "file": MUSIC_CONTENT
        }
        PARAMS = {
            "music_name": MUSIC_NAME
        }

        RESPONCE = requests.post(
            f"{API_URL}/api/v1/bleep_music",
            files=FILE,
            params=PARAMS
        )

        if RESPONCE.status_code == 409:
            self.is_faild_to_bleep_music_exception_raised = True

        elif RESPONCE.status_code == 200:
            self.bleeped_music_url = f"{API_URL}{RESPONCE.json()['bleeped_music_path']}"

    def update_upload_exception_state(self) -> None:
        """
            Updates the `is_music_upload_exception_raised` value
        """
        self.is_music_upload_exception_raised = not self.is_music_upload_exception_raised

    def update_faild_to_bleep_music_exception_state(self) -> None:
        """
            Updates the `is_faild_to_bleep_music_exception_raised` value
        """
        self.is_faild_to_bleep_music_exception_raised = not self.is_faild_to_bleep_music_exception_raised

    def update_playing_state(self) -> None:
        """
            Updates the `is_playing` value
        """
        self.is_playing = not self.is_playing

Note that when calling the function MusBleepState.handle_music_upload(pc.upload_files()) it doesn't get triggered

  • Python Version: 3.10
  • Pynecone Version: 1.22
  • OS: Arch
  • Browser (Optional): Chrome browser (stable)
@ramsy0dev
Copy link
Author

I solved it, it seems like you should not do -> None after the handle_music_upload i still don't know why but i got it

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

No branches or pull requests

1 participant