Skip to content

Solution for visualising PDFs via URL #75

@ArthurTech123

Description

@ArthurTech123

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions