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

[Feature Request] Support for image file type #343

Closed
DhruvThu opened this issue Dec 23, 2022 · 5 comments · Fixed by #347
Closed

[Feature Request] Support for image file type #343

DhruvThu opened this issue Dec 23, 2022 · 5 comments · Fixed by #347

Comments

@DhruvThu
Copy link

Hey, I am new to Robyn framework. Can someone help me to provide an image as response to get request?

Thanks!!

@sansyrox
Copy link
Member

Hey @DhruvThu ,

Right now, you can use something like this:

@app.get("/image")
def return_image():
    return {
        "status": "200",
        "body": Path("test.png").read_bytes(),
        "headers": {"Content-Type": "image/png"},
    }

But this is a good idea, I will create a helper function for it.

@DhruvThu
Copy link
Author

DhruvThu commented Dec 23, 2022

Hey, Thank you so much for quick support. I have tried with the code which you have sent. But I am getting this error.

thread 'actix-rt|system:0|arbiter:0' panicked at 'called Result::unwrap() on an Err value: PyErr { type: <class 'TypeError'>, value: TypeError("'bytes' object cannot be converted to 'PyString'"), traceback: None }', src/executors/mod.rs:89:47

This is the code which I am using.

from robyn import Robyn, static_file
import pathlib
import io

app = Robyn(file)
@app.get("/getimage")
async def h(request):
return {
"status": "200",
"body": pathlib.Path("img.jpg").read_bytes(),
"headers": {"Content-Type": "image/png"},
}

app.start(port=5000)

@sansyrox
Copy link
Member

I think you are using an async function right now. It only works with sync functions right now 😅

@sansyrox sansyrox changed the title [Feature Request] [Feature Request] Support for image file type Dec 23, 2022
@DhruvThu
Copy link
Author

DhruvThu commented Dec 23, 2022

I have tried to run code after removing async. Code is working but I am getting a broken image when I run localhost. My code is as follows:

from robyn import Robyn
from PIL import Image
import pathlib
import numpy as np
import io
import urllib.parse

  
  

app = Robyn(__file__)


@app.get("/getimage")
def return_image(request):
    return {
        "status": "200",
        "body": pathlib.Path("img2.png").read_bytes(),
        "headers": {"Content-Type": "image/png"}
    }

app.start(port=5000)

@sansyrox
Copy link
Member

sansyrox commented Jan 6, 2023

Hey @DhruvThu ,

The issue has been fixed in the latest release. You can find the latest docs here: https://sansyrox.github.io/robyn/#/examples?id=serving-files-to-download

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 a pull request may close this issue.

2 participants