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

Support width in percentage #30 #39

Closed
wants to merge 11 commits into from
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
streamlit
bump-my-version
bump-my-version
lfoppiano marked this conversation as resolved.
Show resolved Hide resolved
streamlit_js_eval
24 changes: 20 additions & 4 deletions streamlit_pdf_viewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Union, List, Optional

import streamlit.components.v1 as components
from streamlit_js_eval import streamlit_js_eval
import json

_RELEASE = True
Expand All @@ -21,8 +22,19 @@
path=build_dir
)

def get_screen_width():
async_js_code = """
new Promise(resolve => {
if (document.readyState === "complete") {
resolve(window.innerWidth);
} else {
window.addEventListener("load", () => resolve(window.innerWidth));
}
})
"""
return streamlit_js_eval(js_expressions=async_js_code)

def pdf_viewer(input: Union[str, Path, bytes], width: int = 700, height: int = None, key=None,
def pdf_viewer(input: Union[str, Path, bytes], width: Union[str, int] = "40%", height: int = None, key=None,
lfoppiano marked this conversation as resolved.
Show resolved Hide resolved
annotations: list = (),
pages_vertical_spacing: int = 2,
annotation_outline_size: int = 1,
Expand Down Expand Up @@ -51,9 +63,13 @@ def pdf_viewer(input: Union[str, Path, bytes], width: int = 700, height: int = N
Returns the value of the selected component (if any).
"""

# Validate width and height parameters
if not isinstance(width, int):
raise TypeError("Width must be an integer")
if isinstance(width, str) and width.endswith('%'):
screen_width = get_screen_width()
percentage = float(width[:-1]) / 100
width = int(screen_width * percentage)
elif not isinstance(width, int):
raise TypeError("Width must be an integer or a percentage string (e.g., '70%' or 700)")

if height is not None and not isinstance(height, int):
raise TypeError("Height must be an integer or None")
if not all(isinstance(page, int) for page in pages_to_render):
Expand Down
Loading