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

Fix regression of version 0.0.8 #38

Merged
merged 12 commits into from
Mar 19, 2024
14 changes: 12 additions & 2 deletions streamlit_pdf_viewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import json

_RELEASE = True
RENDERING_EMBED = "legacy_embed"
RENDERING_IFRAME = "legacy_iframe"
RENDERING_UNWRAP = "unwrap"

if not _RELEASE:
_component_func = components.declare_component(
Expand All @@ -22,11 +25,14 @@
)


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: int = 700,
height: int = None,
key=None,
annotations: list = (),
pages_vertical_spacing: int = 2,
annotation_outline_size: int = 1,
rendering: str = "unwrap",
rendering: str = RENDERING_UNWRAP,
pages_to_render: List[int] = ()
):
"""
Expand Down Expand Up @@ -64,6 +70,10 @@ def pdf_viewer(input: Union[str, Path, bytes], width: int = 700, height: int = N
binary = fo.read()
else:
binary = input

if rendering == RENDERING_IFRAME or rendering == RENDERING_EMBED:
if height is None:
height = "100%"

base64_pdf = base64.b64encode(binary).decode('utf-8')
component_value = _component_func(
Expand Down
7 changes: 4 additions & 3 deletions streamlit_pdf_viewer/frontend/src/PdfViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
</div>
</div>
<div v-else-if="args.rendering==='legacy_embed'">
<embed :src="`data:application/pdf;base64,${args.binary}`" width="100%" height="700" type="application/pdf"/>
<embed :src="`data:application/pdf;base64,${args.binary}`" :width="`${args.width}`" :height="`${args.height}`" type="application/pdf"/>
</div>
<div v-else-if="args.rendering==='legacy_iframe'">
<embed :src="`data:application/pdf;base64,${args.binary}`" width="100%" height="700" type="application/pdf"/>
<embed :src="`data:application/pdf;base64,${args.binary}`" :width="`${args.width}`" :height="`${args.height}`" type="application/pdf"/>
</div>
<div v-else>
Error rendering option.
Expand Down Expand Up @@ -149,7 +149,7 @@

const alertError = (error) => {
window.alert(error.message);
console.error(error);

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.7, 18)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.7, 20)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.8, 18)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.8, 20)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.9, 18)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.9, 20)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.10, 18)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.10, 20)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.11, 18)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.11, 20)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.12, 18)

Unexpected console statement

Check warning on line 152 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.12, 20)

Unexpected console statement
};

const loadPdfs = async (url) => {
Expand All @@ -168,7 +168,6 @@

const setFrameHeight = () => {
Streamlit.setFrameHeight(props.args.height || totalHeight.value);
// Streamlit.setComponentReady();
};

onMounted(() => {
Expand All @@ -177,9 +176,11 @@
loadPdfs(binaryDataUrl);
}
setFrameHeight();
Streamlit.setComponentReady();
});

onUpdated(() => {
// console.log("onUpdated")
setFrameHeight();
});

Expand Down
2 changes: 1 addition & 1 deletion tests/test_iframe_width.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_should_render_template_check_container_size(page: Page):
expect(pdf_container).to_be_visible()

b_box = pdf_container.bounding_box()
assert b_box['width'] == 700
assert b_box['width'] == 500
assert b_box['height'] > 0

pdf_viewer = iframe_frame.locator('div[id="pdfViewer"]')
Expand Down
Loading