Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
B4PT0R committed Dec 27, 2023
0 parents commit ad651e1
Show file tree
Hide file tree
Showing 16 changed files with 29,909 additions and 0 deletions.
143 changes: 143 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
doc/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include the Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# Streamlit components
frontend/build
node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018-2021 Streamlit Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include my_component/frontend/build *
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# streamlit-pdf-viewer

Streamlit pdf reader component

## Installation instructions

```sh
pip install streamlit-pdf-reader
```

## Usage instructions

```python
pdf_reader(source)
```

`source` can be either a local pdf file, a pdf file url, or a BytesIO

## Example
```python
import streamlit as st
from streamlit_pdf_reader import pdf_reader

source1='./test.pdf'
pdf_reader(source1)

source2="https://www-fourier.ujf-grenoble.fr/~faure/enseignement/relativite/cours.pdf"
pdf_reader(source2)

source3=st.file_uploader("Choose a pdf file:")
if source3:
pdf_reader(source3)

st.button("Rerun")
```
27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pathlib import Path

import setuptools

this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text()

setuptools.setup(
name="streamlit_pdf_reader",
version="0.0.1",
author="Baptiste Ferrand",
author_email="[email protected]",
description="Streamlit pdf reader component",
long_description=long_description,
long_description_content_type="text/markdown",
url="",
packages=setuptools.find_packages(),
include_package_data=True,
classifiers=[],
python_requires=">=3.7",
install_requires=[
# By definition, a Custom Component depends on Streamlit.
# If your component has other Python dependencies, list
# them here.
"streamlit >= 0.63",
],
)
61 changes: 61 additions & 0 deletions streamlit_pdf_reader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import base64
import streamlit.components.v1 as components
import requests
from pathlib import Path
from io import BytesIO

_RELEASE = True

if not _RELEASE:
_component_func = components.declare_component("streamlit_pdf_reader",url="http://localhost:3001")
else:
parent_dir = os.path.dirname(os.path.abspath(__file__))
build_dir = os.path.join(parent_dir, "frontend/build")
_component_func = components.declare_component("streamlit_pdf_reader", path=build_dir)

def load_pdf(source):
"""Load a PDF from various sources and return as a Base64 encoded string."""

if isinstance(source, str) and source.startswith('http'):
response = requests.get(source)
response.raise_for_status()
pdf_bytes = response.content
elif isinstance(source, str) and Path(source).is_file():
with open(source, 'rb') as file:
pdf_bytes = file.read()
elif isinstance(source, BytesIO):
pdf_bytes = source.getvalue()
else:
raise ValueError("Invalid source type for PDF.")

# Encode to Base64 and prepend MIME type
base64_pdf = base64.b64encode(pdf_bytes).decode()
full_base64_string = f'data:application/pdf;base64,{base64_pdf}'

return full_base64_string

def pdf_reader(source, key=None):
"""Streamlit component to display a PDF from various sources."""

# Encode the source to Base64
base64_string = load_pdf(source)

# Call the frontend component
return _component_func(base64_string=base64_string, key=key)


if not _RELEASE:
import streamlit as st

source1='./test.pdf'
pdf_reader(source1)

source2="https://www-fourier.ujf-grenoble.fr/~faure/enseignement/relativite/cours.pdf"
pdf_reader(source2)

source3=st.file_uploader("Choose a pdf file:")
if source3:
pdf_reader(source3)

st.button("Rerun")
5 changes: 5 additions & 0 deletions streamlit_pdf_reader/frontend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"endOfLine": "lf",
"semi": false,
"trailingComma": "es5"
}
Loading

0 comments on commit ad651e1

Please sign in to comment.