pythonflet

estimation Internet Speed test app in flet


i have completly followed to the following link :youtube tutorial for speed test

and also given link : github page

and there is my code :

import flet  as ft
from flet import *
from time import sleep
import speedtest
def main(page:ft.Page):
    page.title ="Internet Speed Test"
    page.theme_mode ="dark"
    page.vertical_alignment ="center"
    page.horizontal_alignment ="center"
    page.window.bgcolor ="blue"
    page.padding =30
    page.bgcolor ="black"
    page.auto_scroll =True
    page.fonts={
        "RoosterPersonalUse":"fonts/RoosterPersonalUse-3z8d8.ttf",
        "SourceCodePro-BlackItalic":"fonts/SourceCodePro-BlackItalic.ttf",
        "SourceCodePro-Bold" :"fonts/SourceCodePro-Bold.ttf"
    }
    st =speedtest.Speedtest(secure=True)
    appTitle =ft.Row(
        controls=[
            ft.Text(value="Internet",font_family="RoosterPersonalUse",
                    style=ft.TextThemeStyle(value="displayLarge"),color="#ff3300"),
            ft.Text(value ="Speed",font_family="SourceCodePro-BlackItalic",
                    style=ft.TextThemeStyle(value="displayLarge"),color="#ffff00")
        ],alignment=ft.MainAxisAlignment(value="center")
    )
    line_01 = ft.Text(value="> press start...", font_family="SourceCodePro-BlackItalic", color="#ffffff")
    line_02 = ft.Text(value="", font_family="SourceCodePro-BlackItalic",  color="#1aff1a")
    line_03 = ft.Text(value="", font_family="SourceCodePro-BlackItalic",  color="#1aff1a")
    line_04 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#ffff00")
    line_05 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
    line_06 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
    line_07 = ft.Text(value="", font_family="SourceCodePro-BlackItalic",color="#ffff00")
    line_08 = ft.Text(value="", font_family="SourceCodePro-BlackItalic",  color="#ffffff")
    progress_bar_01 =ft.ProgressBar(width=400,color="#0080ff", bgcolor="#eeeeee", opacity=0)
    progress_text_01 = ft.Text(" ", font_family="SourceCodePro-BlackItalic", color="#1aff1a",
                                         )
    progress_row_01 = ft.Row([progress_text_01, progress_bar_01])
    progress_bar_02 = ft.ProgressBar(width=400, color="#0080ff", bgcolor="#eeeeee", opacity=0)
    progress_text_02 = ft.Text(" ", font_family="SourceCodePro-BlackItalic", color="#1aff1a",
                                        )
    progress_row_02 = ft.Row([progress_text_02, progress_bar_02])
    terminalText = ft.Column(
        [line_01, line_02, line_03, progress_row_01, line_04, line_05, line_06, progress_row_02, line_07, line_08])




    getSpeedContainer =ft.Container(
        content=terminalText,
        width=200,
        height=100,
        bgcolor="#4d4d4d",
        border_radius=30,
        padding=20,
        animate=ft.Animation(duration=1000,curve=ft.AnimationCurve(value="bounceOut"))


    )
    def animate_getSpeedContainer(e):
        getSpeedContainer.update()
        progress_row_01.opacity = 0
        progress_bar_01.opacity = 0
        progress_bar_01.value = None
        progress_row_02.opacity = 0
        progress_bar_02.opacity = 0
        progress_bar_02.value = None
        line_01.text_to_print = ""
        line_01.update()
        line_02.text_to_print = ""
        line_02.update()
        line_03.text_to_print = ""
        line_03.update()
        line_04.text_to_print = ""
        line_04.update()
        line_05.text_to_print = ""
        line_05.update()
        line_06.text_to_print = ""
        line_06.update()
        line_07.text_to_print = ""
        line_07.update()
        line_08.text_to_print = ""
        line_08.update()
        getSpeedContainer.width =700
        getSpeedContainer.height =400
        getSpeedContainer.update()
        getSpeedContainer.update()
        getSpeedContainer.width = 700
        getSpeedContainer.height = 400
        line_01.text_to_print = "> calculating download speed, please wait..."
        getSpeedContainer.update()
        sleep(1)
        line_01.update()
        ideal_server = st.get_best_server()  # this will find out and connect to the best possible server
        city = ideal_server["name"]  # for getting the city name
        country = ideal_server["country"]  # for getting the country name
        cc = ideal_server["cc"]  # for getting the country code
        line_02.text_to_print = f"> finding the best possible servers in {city}, {country} ({cc})"
        line_02.update()
        getSpeedContainer.update()
        sleep(1)
        line_03.text_to_print = "> connection established, status OK, fetching download speed"
        line_03.update()
        progress_row_01.opacity = 1
        progress_bar_01.opacity = 1
        getSpeedContainer.update()
        download_speed = st.download() / 1024 / 1024  # bytes/sec to Mbps
        progress_bar_01.value = 1
        line_04.text_to_print = f"> the download speed is {str(round(download_speed, 2))} Mbps"
        line_04.update()
        getSpeedContainer.update()
        line_05.text_to_print = "> calculating upload speed, please wait..."
        line_05.update()
        getSpeedContainer.update()
        sleep(1)
        line_06.text_to_print = "> executing upload script, hold on"
        line_06.update()
        progress_row_02.opacity = 1
        progress_bar_02.opacity = 1
        getSpeedContainer.update()
        upload_speed = st.upload() / 1024 / 1024  # bytes/sec to Mbps
        progress_bar_02.value = 1
        line_07.text_to_print = f"> the upload speed is {str(round(upload_speed, 2))} Mbps"
        line_07.update()
        getSpeedContainer.update()
        sleep(1)
        line_08.text_to_print = f"> task completed successfully\n\n>> app developer: kumar anurag (instagram: kmranrg)"
        line_08.update()
        getSpeedContainer.update()

    page.add(
        appTitle,
        getSpeedContainer,
        ft.IconButton(icon=ft.Icons.PLAY_CIRCLE_FILL_OUTLINED,icon_color="green",icon_size=70,
                      on_click=animate_getSpeedContainer),

    )

ft.app(target=main,assets_dir="assets")

result of this code is given as : enter image description here

as you can see texts are not displayed and also reseults of speeds are not calculated, could you tell me please reason for it? thanks in advance


Solution

  • There were multiple issues with your code.

    Fixes:

    1. .text_to_print used incorrectly. Replaced with .value

    2. speedtest might silently fail. Added try/except for debug

    3. Font might not load. Used system font or ensure asset path is correct.

    4. sleep() might block UI. Used asyncio.sleep() if using async version later

    Here is the corrected code:

    import flet as ft
    from flet import *
    from time import sleep
    import speedtest
    
    def main(page: ft.Page):
        page.title = "Internet Speed Test"
        page.theme_mode = "dark"
        page.vertical_alignment = "center"
        page.horizontal_alignment = "center"
        page.window.bgcolor = "blue"
        page.padding = 30
        page.bgcolor = "black"
        page.auto_scroll = True
        page.fonts = {
            "RoosterPersonalUse": "fonts/RoosterPersonalUse-3z8d8.ttf",
            "SourceCodePro-BlackItalic": "fonts/SourceCodePro-BlackItalic.ttf",
            "SourceCodePro-Bold": "fonts/SourceCodePro-Bold.ttf"
        }
    
        st = speedtest.Speedtest(secure=True)
    
        appTitle = ft.Row(
            controls=[
                ft.Text(value="Internet", font_family="RoosterPersonalUse",
                        style=ft.TextThemeStyle.DISPLAY_LARGE, color="#ff3300"),
                ft.Text(value="Speed", font_family="SourceCodePro-BlackItalic",
                        style=ft.TextThemeStyle.DISPLAY_LARGE, color="#ffff00")
            ],
            alignment=ft.MainAxisAlignment.CENTER
        )
    
        line_01 = ft.Text(value="> press start...", font_family="SourceCodePro-BlackItalic", color="#ffffff")
        line_02 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
        line_03 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
        line_04 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#ffff00")
        line_05 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
        line_06 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
        line_07 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#ffff00")
        line_08 = ft.Text(value="", font_family="SourceCodePro-BlackItalic", color="#ffffff")
    
        progress_bar_01 = ft.ProgressBar(width=400, color="#0080ff", bgcolor="#eeeeee", opacity=0)
        progress_text_01 = ft.Text(" ", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
        progress_row_01 = ft.Row([progress_text_01, progress_bar_01])
    
        progress_bar_02 = ft.ProgressBar(width=400, color="#0080ff", bgcolor="#eeeeee", opacity=0)
        progress_text_02 = ft.Text(" ", font_family="SourceCodePro-BlackItalic", color="#1aff1a")
        progress_row_02 = ft.Row([progress_text_02, progress_bar_02])
    
        terminalText = ft.Column([
            line_01, line_02, line_03, progress_row_01,
            line_04, line_05, line_06, progress_row_02,
            line_07, line_08
        ])
    
        getSpeedContainer = ft.Container(
            content=terminalText,
            width=200,
            height=100,
            bgcolor="#4d4d4d",
            border_radius=30,
            padding=20,
            animate=ft.Animation(duration=1000, curve=ft.AnimationCurve.BOUNCE_OUT)
        )
    
        def animate_getSpeedContainer(e):
            # Reset
            progress_row_01.opacity = 0
            progress_bar_01.opacity = 0
            progress_bar_01.value = None
    
            progress_row_02.opacity = 0
            progress_bar_02.opacity = 0
            progress_bar_02.value = None
    
            for line in [line_01, line_02, line_03, line_04, line_05, line_06, line_07, line_08]:
                line.value = ""
                line.update()
    
            getSpeedContainer.width = 700
            getSpeedContainer.height = 400
            getSpeedContainer.update()
    
            line_01.value = "> calculating download speed, please wait..."
            line_01.update()
            sleep(1)
    
            ideal_server = st.get_best_server()
            city = ideal_server["name"]
            country = ideal_server["country"]
            cc = ideal_server["cc"]
    
            line_02.value = f"> finding the best possible servers in {city}, {country} ({cc})"
            line_02.update()
            sleep(1)
    
            line_03.value = "> connection established, status OK, fetching download speed"
            line_03.update()
            progress_row_01.opacity = 1
            progress_bar_01.opacity = 1
            getSpeedContainer.update()
    
            download_speed = st.download() / 1024 / 1024
            progress_bar_01.value = 1
            progress_bar_01.update()
            line_04.value = f"> the download speed is {round(download_speed, 2)} Mbps"
            line_04.update()
    
            line_05.value = "> calculating upload speed, please wait..."
            line_05.update()
            sleep(1)
    
            line_06.value = "> executing upload script, hold on"
            line_06.update()
            progress_row_02.opacity = 1
            progress_bar_02.opacity = 1
            getSpeedContainer.update()
    
            upload_speed = st.upload() / 1024 / 1024
            progress_bar_02.value = 1
            progress_bar_02.update()
            line_07.value = f"> the upload speed is {round(upload_speed, 2)} Mbps"
            line_07.update()
            sleep(1)
    
            line_08.value = f"> task completed successfully\n\n>> app developer: kumar anurag (instagram: kmranrg)"
            line_08.update()
            getSpeedContainer.update()
    
        page.add(
            appTitle,
            getSpeedContainer,
            ft.IconButton(
                icon=ft.Icons.PLAY_CIRCLE_FILL_OUTLINED,
                icon_color="green",
                icon_size=70,
                on_click=animate_getSpeedContainer
            )
        )
    
    ft.app(target=main, assets_dir="assets")
    

    Output:

    enter image description here