Skip to content

Commit b38b345

Browse files
authored
Add slugify to utils (#154)
1 parent 9fa4839 commit b38b345

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

sdk/python/flet/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import platform
33
import sys
4+
import unicodedata
45
import webbrowser
56

67

@@ -82,3 +83,23 @@ def is_localhost_url(url):
8283
def get_current_script_dir():
8384
pathname = os.path.dirname(sys.argv[0])
8485
return os.path.abspath(pathname)
86+
87+
88+
def slugify(original: str) -> str:
89+
"""
90+
Make a string url friendly. Useful for creating routes for navigation.
91+
92+
>>> slugify("What's up?")
93+
'whats-up'
94+
95+
>>> slugify(" Mitä kuuluu? ")
96+
'mitä-kuuluu'
97+
"""
98+
slugified = original.strip()
99+
slugified = " ".join(slugified.split()) # Remove extra spaces between words
100+
slugified = slugified.lower()
101+
# Remove unicode punctuation
102+
slugified = "".join(character for character in slugified if not unicodedata.category(character).startswith("P"))
103+
slugified = slugified.replace(" ", "-")
104+
105+
return slugified

0 commit comments

Comments
 (0)