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 issue caused by new script cache in 1.23.1 #49

Merged
merged 32 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
75baa30
Fix issue caused by new script cache in 1.23.1
mfriedy Jun 16, 2023
a32db94
Fix import location
mfriedy Jun 16, 2023
0b870b6
Merge branch 'add-py-typed' into streamlit1231_compat
blackary Jul 5, 2023
f28b952
Use built-in absolute
blackary Jul 5, 2023
5953298
Add quick sleep
blackary Jul 5, 2023
d3ed0f2
Add py.typed
blackary Jun 6, 2023
412609b
Bump version number
blackary Jun 6, 2023
cb15ede
Fix set config, update tests
blackary Jul 5, 2023
e09964c
Update linters
blackary Jul 5, 2023
8db40e1
Bump version
blackary Jul 5, 2023
b97bbe3
Use built-in absolute
blackary Jul 5, 2023
a053bd5
Merge branch 'script-cache-fix' into streamlit1231_compat
blackary Jul 5, 2023
ed912ff
Add tests
blackary Jul 5, 2023
175aa9d
Update versions supported
blackary Jul 5, 2023
91bc27f
Add sleep before checking indentation
blackary Jul 5, 2023
9e3ffee
Bump timeout
blackary Jul 5, 2023
8210bfb
Even longer?
blackary Jul 5, 2023
56f25d7
Merge branch 'main' of https://github.com/blackary/st_pages into stre…
mfriedy Aug 1, 2023
8424c36
Merge branch 'blackary:main' into streamlit1231_compat
mfriedy Aug 2, 2023
bba8bb7
Remove numpy
blackary Aug 11, 2023
c6c876e
Bump streamlit version
blackary Aug 23, 2023
fb997e8
Hard-code emoji file rather than pulling
blackary Aug 23, 2023
4e8af30
Update versions
blackary Aug 23, 2023
6e2ca06
Bump version
blackary Aug 23, 2023
e1ffe05
Add git ignore
mfriedy Aug 25, 2023
0eb68e0
Merge branch 'streamlit1231_compat' of https://github.com/mfriedy/st_…
mfriedy Aug 25, 2023
1c41d5b
Bugfix: specify utf-8 encoding for windows compatability
mfriedy Aug 25, 2023
724c0e1
Merge branch 'main' into streamlit1231_compat
blackary Sep 29, 2023
483b8a8
Merge branch 'main' into streamlit1231_compat
blackary Sep 29, 2023
466f50a
Add cleanup after test
blackary Sep 29, 2023
9330376
Add screenshots, upload to github
blackary Sep 29, 2023
b9d0894
Add longer sleep
blackary Sep 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ jobs:
playwright install --with-deps
- name: Test with tox
run: tox
- uses: actions/upload-artifact@v3
if: failure()
with:
name: screenshots
path: screenshot*.png
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ __pycache__

.streamlit/secrets.toml

*.png
*.png
.venv
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Releases](https://img.shields.io/pypi/v/st-pages)](https://pypi.org/project/st-pages/)
[![Build Status](https://img.shields.io/github/actions/workflow/status/blackary/st_pages/testing.yml?branch=main)](https://github.com/blackary/st_pages/actions?query=workflow%3A%22testing%22+branch%3Amain)
![Python Versions](https://img.shields.io/pypi/pyversions/st_pages.svg)
![Streamlit versions](https://img.shields.io/badge/streamlit-1.15.0--1.18.0-white.svg)
![Streamlit versions](https://img.shields.io/badge/streamlit-1.21.0--1.24.0-white.svg)
![License](https://img.shields.io/github/license/blackary/st_pages)
[![Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Expand Down
17 changes: 15 additions & 2 deletions src/st_pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
from dataclasses import dataclass
from pathlib import Path
from time import sleep

import toml

Expand All @@ -15,8 +16,10 @@ def _gather_metrics(name, func, *args, **kwargs):


import streamlit as st
from streamlit import runtime
from streamlit.commands.page_config import get_random_emoji
from streamlit.errors import StreamlitAPIException
from streamlit.watcher import LocalSourcesWatcher

try:
from streamlit.runtime.scriptrunner import get_script_run_ctx
Expand Down Expand Up @@ -108,7 +111,7 @@ def _add_page_title(
@cache_resource
def get_icons() -> dict[str, str]:
emoji_path = Path(__file__).parent / "emoji.json"
return json.loads(emoji_path.read_text())
return json.loads(emoji_path.read_text(encoding="utf-8"))


def translate_icon(icon: str) -> str:
Expand Down Expand Up @@ -187,7 +190,7 @@ def to_dict(self) -> dict[str, str | bool]:
"page_script_hash": self.page_hash,
"page_name": self.page_name,
"icon": self.page_icon,
"script_path": str(self.page_path),
"script_path": str(self.page_path.absolute()),
"is_section": self.is_section,
"in_section": self.in_section,
"relative_page_hash": self.relative_page_hash,
Expand Down Expand Up @@ -241,6 +244,16 @@ def _show_pages(pages: list[Page]):

_on_pages_changed.send()

rt = runtime.get_instance()

if hasattr(rt, "_script_cache"):
sleep(1) # Not sure why this is needed, but it seems to be.

rt._sources_watcher = LocalSourcesWatcher(rt._main_script_path)
rt._sources_watcher.register_file_change_callback(
lambda _: rt._script_cache.clear()
)


show_pages = _gather_metrics("st_pages.show_pages", _show_pages)

Expand Down
98 changes: 98 additions & 0 deletions tests/test_edits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from contextlib import contextmanager
from pathlib import Path
from time import sleep

import pytest
from playwright.sync_api import Page, expect

LOCAL_TEST = False

PORT = "8503" if LOCAL_TEST else "8699"


@contextmanager
def run_streamlit():
"""Run the streamlit app at examples/streamlit_app.py on port 8599"""
import subprocess

if LOCAL_TEST:
try:
yield 1
finally:
pass
else:
p = subprocess.Popen(
[
"streamlit",
"run",
"example_app/streamlit_app.py",
"--server.port",
PORT,
"--server.headless",
"true",
"--server.runOnSave",
"true",
]
)

sleep(5)

try:
yield 1
finally:
p.kill()


@pytest.fixture(scope="module", autouse=True)
def before_module():
# Run the streamlit app before each module
with run_streamlit():
yield

Path("example_app/example_one.py").write_text(
"""import streamlit as st
from st_pages import add_page_title

add_page_title(layout="wide")

st.write("This is just a sample page!")
"""
)


@pytest.fixture(scope="function", autouse=True)
def before_test(page: Page):
page.goto(f"localhost:{PORT}")

yield

TEST_TEXT = "THIS IS A TEST"

current_text = Path("example_app/example_one.py").read_text()

with Path("example_app/example_one.py").open("w") as f:
f.write(current_text.replace(f"\nst.write('{TEST_TEXT}')\n", ""))


def test_page_update(page: Page):
TEST_TEXT = "THIS IS A TEST"
page.get_by_role("link", name="Example One").click()

expect(page).to_have_title("Example One")

page.screenshot(path="screenshot-edits0.png", full_page=True)

try:
expect(page.get_by_text(TEST_TEXT)).not_to_be_visible()
except Exception as e:
page.screenshot(path="screenshot-edits1.png", full_page=True)
raise e

with Path("example_app/example_one.py").open("a") as f:
f.write(f"\nst.write('{TEST_TEXT}')\n")

try:
expect(page.get_by_text(TEST_TEXT)).to_be_visible()
except Exception as e:
page.screenshot(path="screenshot-edits2.png", full_page=True)
raise e
8 changes: 7 additions & 1 deletion tests/test_frontend_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def test_deprecation_warning(page: Page):


def test_in_section_false(page: Page):
sleep(3)
page.screenshot(path="screenshot_sections2.png")
bbox_not_in_section = (
page.get_by_role("link", name="Example Five")
.get_by_text("Example Five")
Expand All @@ -99,7 +101,11 @@ def test_in_section_false(page: Page):

# Check that the in_section=False page is at least 10 pixels to the left of the
# in_section=True page
assert bbox_not_in_section["x"] < bbox_in_section["x"] - 10
try:
assert bbox_not_in_section["x"] < bbox_in_section["x"] - 10
except AssertionError as e:
page.screenshot(path="screenshot_sections3.png")
raise e


def test_page_hiding(page: Page):
Expand Down
Loading