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

Trying to make the output download an excel file or display a dataframe #29

Closed
batman984 opened this issue May 3, 2021 · 5 comments
Closed
Labels
support Usage or installation support is requested

Comments

@batman984
Copy link

Hi,
I have been doodling with opyrator, I think it is awesome.
I got stuck in trying to make the output for the user is to download an excel file generated following the model you have in upscaling the image and spleeter example
`
class HelixPredictOutput(BaseModel):
upscaled_image_file: FileContent = Field(
...,
mime_type="application/excel",
description="excel file download",
)

def image_super_resolution(
input: HelixPredictInput,
) -> HelixPredictOutput:

with open("aa.xlsx", "rb") as f:
            excel_outfile = f.read()

return HelixPredictOutput(
            upscaled_image_file=excel_outfile)

`
But that gave me a weird zip file when I press on the download button that was unsupported. Is there anyway to make it download the excel file?

@batman984 batman984 added the support Usage or installation support is requested label May 3, 2021
@raethlein
Copy link
Member

Hey @batman984,

based on your example I have created this file:

from opyrator.components.types import FileContent
from pydantic import BaseModel, Field, validator


class Input(BaseModel):
    pass


class Output(BaseModel):
    excel_file: FileContent = Field(
        ...,
        mime_type="application/excel",
        description="excel download",
    )


def call(input: Input) -> Output:
    with open("test.xlsx", "rb") as f:
        excel_outfile = f.read()
    return Output(excel_file=excel_outfile)

Though, when I call the /call endpoint, I get a .json file and not a .zip file. That json file has a field called excel_file (as declared in the Output class) that contains the content of the excel field base64 encoded. So, using for example curl in the terminal, the following call gives me the excel file:

curl -X 'POST' \
  'http://localhost:8080/call' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{}' | jq -r '.excel_file' | base64 --decode > downloaded_test.xlsx

Unfortunately, this does not help with directly downloading it from the Swagger UI yet.

@batman984
Copy link
Author

Aww .. bummer .. Hope it maybe supported in the future

@LukasMasuch
Copy link
Contributor

@batman984 The download might actually already give you the correct file, but just the file extension is missing. Currently, the file extension is determined by the specified mime type. The official mimetype for xlsx files is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. You might try it with this mime type instead of application/excel. Here is an example:

from opyrator.components.types import FileContent
from pydantic import BaseModel, Field, validator


class Input(BaseModel):
    excel_file: FileContent = Field(
        ...,
        mime_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )


class Output(BaseModel):
    excel_file: FileContent = Field(
        ...,
        mime_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )


def return_file(input: Input) -> Output:
    return Output(excel_file=input.excel_file.as_bytes())

@raethlein
Copy link
Member

raethlein commented May 5, 2021

Ah, great that works! My post was about the Swagger API (via launch-api) where you can also download the response, but I guess the question was raised in the context of the Streamlit UI which probably makes more sense. Sorry @batman984 for pointing you to the wrong direction previously.

@batman984
Copy link
Author

That worked .. thank you so much @LukasMasuch .. closing the issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Usage or installation support is requested
Projects
None yet
Development

No branches or pull requests

3 participants