-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #963 from zauberzeug/improved-favicon-routing
Fixed and improved favicon routing
- Loading branch information
Showing
7 changed files
with
138 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pytest-selenium | |
pytest-asyncio | ||
selenium | ||
autopep8 | ||
icecream | ||
icecream | ||
beautifulsoup4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
from nicegui import favicon, ui | ||
|
||
from .screen import PORT, Screen | ||
|
||
DEFAULT_FAVICON_PATH = Path(__file__).parent.parent / 'nicegui' / 'static' / 'favicon.ico' | ||
LOGO_FAVICON_PATH = Path(__file__).parent.parent / 'website' / 'static' / 'logo_square.png' | ||
|
||
|
||
def assert_favicon_url_starts_with(screen: Screen, content: str): | ||
soup = BeautifulSoup(screen.selenium.page_source, 'html.parser') | ||
icon_link = soup.find("link", rel="icon") | ||
assert icon_link['href'].startswith(content) | ||
|
||
|
||
def assert_favicon(content: Union[Path, str, bytes], url_path: str = '/favicon.ico'): | ||
response = requests.get(f'http://localhost:{PORT}{url_path}') | ||
assert response.status_code == 200 | ||
if isinstance(content, Path): | ||
assert content.read_bytes() == response.content | ||
elif isinstance(content, str): | ||
assert content == response.text | ||
elif isinstance(content, bytes): | ||
assert content == response.content | ||
else: | ||
raise TypeError(f'Unexpected type: {type(content)}') | ||
|
||
|
||
def test_default(screen: Screen): | ||
ui.label('Hello, world') | ||
|
||
screen.open('/') | ||
assert_favicon(DEFAULT_FAVICON_PATH) | ||
|
||
|
||
def test_emoji(screen: Screen): | ||
ui.label('Hello, world') | ||
|
||
screen.ui_run_kwargs['favicon'] = '👋' | ||
screen.open('/') | ||
assert_favicon_url_starts_with(screen, 'data:image/svg+xml') | ||
assert_favicon(favicon.char_to_svg('👋')) | ||
|
||
|
||
def test_data_url(screen: Screen): | ||
ui.label('Hello, world') | ||
|
||
icon = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' | ||
screen.ui_run_kwargs['favicon'] = icon | ||
screen.open('/') | ||
assert_favicon_url_starts_with(screen, 'data:image/png;base64') | ||
_, bytes = favicon.data_url_to_bytes(icon) | ||
assert_favicon(bytes) | ||
|
||
|
||
def test_custom_file(screen: Screen): | ||
ui.label('Hello, world') | ||
|
||
screen.ui_run_kwargs['favicon'] = LOGO_FAVICON_PATH | ||
screen.open('/') | ||
assert_favicon_url_starts_with(screen, '/favicon.ico') | ||
assert_favicon(screen.ui_run_kwargs['favicon']) | ||
|
||
|
||
def test_page_specific_icon(screen: Screen): | ||
@ui.page('/subpage', favicon=LOGO_FAVICON_PATH) | ||
def sub(): | ||
ui.label('Subpage') | ||
|
||
ui.label('Main') | ||
|
||
screen.open('/subpage') | ||
assert_favicon(LOGO_FAVICON_PATH, url_path='/subpage/favicon.ico') | ||
screen.open('/') | ||
|
||
|
||
def test_page_specific_emoji(screen: Screen): | ||
@ui.page('/subpage', favicon='👋') | ||
def sub(): | ||
ui.label('Subpage') | ||
|
||
ui.label('Main') | ||
|
||
screen.open('/subpage') | ||
assert_favicon_url_starts_with(screen, 'data:image/svg+xml') | ||
screen.open('/') | ||
assert_favicon(DEFAULT_FAVICON_PATH) |