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

Can not support url str and the solution #75

Open
ArthurTech123 opened this issue Nov 1, 2024 · 1 comment
Open

Can not support url str and the solution #75

ArthurTech123 opened this issue Nov 1, 2024 · 1 comment

Comments

@ArthurTech123
Copy link

ArthurTech123 commented Nov 1, 2024

version information:

  • streamlit_pdf_viewer: 0.0.19
  • python: 3.11.7
  • streamlit: 1.39.0

problem

I try to input my s3 pdf file path, but get the error from terminal

Traceback (most recent call last):
  File "c:\Users\Demo\Desktop\GitHub\demo\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "c:\Users\Demo\Desktop\GitHub\demo\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
  File "C:\Users\Demo\Desktop\GitHub\demo\app.py", line 539, in <module>
    Flow(
  File "C:\Users\Demo\Desktop\GitHub\demo\app.py", line 462, in __init__
    self.set_content()
  File "C:\Users\Demo\Desktop\GitHub\demo\app.py", line 505, in set_content
    ).run()
      ^^^^^
  File "c:\Users\Demo\Desktop\GitHub\demo\.venv\Lib\site-packages\streamlit\navigation\page.py", line 293, in run
    self._page()
  File "C:\Users\Demo\Desktop\GitHub\demo\app.py", line 395, in portfolio
    pdf_viewer(
  File "c:\Users\Demo\Desktop\GitHub\demo\.venv\Lib\site-packages\streamlit_pdf_viewer\__init__.py", line 93, in pdf_viewer
    with open(input, 'rb') as fo:
         ^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument: 'https://demo.com/0.pdf'

solution

file location: site-packages/streamlit_pdf_viewer/init.py

modify the code

import requests

    if type(input) is not bytes:
        try: # give a try except on original function
            with open(input, 'rb') as fo:
                binary = fo.read()
        except:
            try: # try to get url file with requests
                response = requests.get(input)
                response.raise_for_status()
                binary = response.content
            except: # if still failed raise error message
                raise TypeError(f"input must be a file path, URL, or binary data. {input} is not a valid file path or URL.")
    else:
        binary = input
@lfoppiano
Copy link
Owner

lfoppiano commented Nov 2, 2024

Thanks! We had an issue opened for this (#33), but we've decided to leave this to the developer because it might require to cache the PDF otherwise at each click the pdf viewer will try to download 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

2 participants