import flet as ft import pyshorteners # pip install pyshorteners shortener = pyshorteners.Shortener() class ShortLinkRow(ft.Row): # a row containing the shortened url, and two buttons ('copy', and 'open in browser') def __init__(self, shortened_link, link_source): """ We create a new class called `ShortenedLinkRow` that inherits from `ft.Row`. The constructor takes two arguments/parameters: `shortened_link` and `source`. :param shortened_link: the shortened link :param link_source: the service hosting the shortened_link """ super().__init__() # required when overwriting the constructor self.tooltip = link_source # set the tooltip of the row itself to the link provider/source self.alignment = "center" # center the contents of this row # the controls/content of our Row self.controls = [ ft.Text(value=shortened_link, size=16, selectable=True, italic=True), ft.IconButton( icon=ft.icons.COPY, # the icon to be showed on_click=lambda e: self.copy(shortened_link), # when this button is clicked, call the `copy` method, passing the shortened link as parameter bgcolor=ft.colors.BLUE_700, tooltip="copy" # to be showed when hovering on this button ), ft.IconButton( icon=ft.icons.OPEN_IN_BROWSER_OUTLINED, # the icon to be showed tooltip="open in browser", # to be showed when hovering on this button on_click=lambda e: e.page.launch_url(shortened_link) # when this button is clicked, open a browser tab with that shortened link ) ] def copy(self, value): """ It copies the given value to the clipboard, and opens a Snackbar to inform the user. :param value: The value to be copied to the clipboard """ self.page.set_clipboard(value) self.page.show_snack_bar( ft.SnackBar( ft.Text("Link copied to clipboard!"), open=True ) ) def main(page: ft.Page): page.theme_mode = "light" # light theme/mode page.title = "URL Shortener" # title of application/page page.horizontal_alignment = "center" # center our page's content (remove or change at wish) page.appbar = ft.AppBar( title=ft.Text("URL Shortener", color=ft.colors.WHITE), # title of the AppBar, with a white color center_title=True, # we center the title bgcolor=ft.colors.BLUE, # a color for the AppBar's background ) def shorten(e): """Grabs the URL in the textfield, and displays shortened versions of it.""" user_link = text_field.value # retrieve the content of the textfield if user_link: # if the textfield is not empty # if the entered text in the textfield is not a valid URl, the program may break, hence the need to catch that try: page.add(ft.Text(f"Long URL: {user_link}", italic=False, weight='bold')) page.add(ShortLinkRow(shortened_link=shortener.tinyurl.short(user_link), link_source="By tinyurl.com")) page.add(ShortLinkRow(shortened_link=shortener.chilpit.short(user_link), link_source="By chilp.it")) page.add(ShortLinkRow(shortened_link=shortener.clckru.short(user_link), link_source="By clck.ru")) page.add(ShortLinkRow(shortened_link=shortener.dagd.short(user_link), link_source="By da.dg")) except Exception as exception: # the error might be that a url shortening service from pyshorteners failed to shorten our url print(exception) # inform the user that an error has occurred page.show_snack_bar( ft.SnackBar( ft.Text("Sorry, but an error occurred. Please retry, or refresh the page."), open=True ) ) else: # if the textfield is empty (no text) # inform the user page.show_snack_bar( ft.SnackBar( ft.Text("Please enter a URL in the field!"), open=True ) ) text_field = ft.TextField( value='https://github.com/ndonkoHenri', # a test link label="Long URL", # the field's label hint_text="type long url here", # the field's hint-text max_length=200, # the maximum length of inputted links keyboard_type="url", # the field's keyboard type # 'shorten' is the function to be called on occurrence of some events suffix=ft.FilledButton("Shorten!", on_click=shorten), # event: button clicked on_submit=shorten # event: 'enter' key pressed ) page.add( text_field, ft.Text("Generated URLs:", weight="bold", size=23) ) # add our textfield to the page/UI ft.app(target=main, view=ft.WEB_BROWSER)