1

I'm writing a script using Flet and I need to write a script to restart the app, but I don't know how to implement it.

Here's the code:

def theme_changed(e):
        print(data["theme"])
        data["theme"] = str(theme_dd.value)
        print(data["theme"])

        def close_app(e):
            page.banner.open = False
            #Here I need the code
        def restar_late(e):
            page.banner.open = False
            page.update()

        page.banner = ft.Banner(leading=ft.Icon(ft.icons.SETTINGS,color=ft.colors.RED_100,size=40),content=
            ft.Text("Please restart the application\nto apply the changes",font_family=font),actions=[
                ft.ElevatedButton("Restart Now",style=buttonsStyle,on_click=close_app),
                ft.ElevatedButton("Restart Late",style=buttonsStyle,on_click=restar_late)
            ],open=True)
        page.update()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KirilAf
  • 11
  • 1
  • Long story short (but your own use case may not require it and given answer may suffice): if you’re running say a webserver that has to stay up but needs restarting, you may need a class of program that manages other processes, starts them, keeps themup if they fail, restarts them - https://en.wikipedia.org/wiki/Init - that might be`systemd` on Linux, `Service Manager` on Windows…. Again, *you* may not need this, but mentioning just in case. – JL Peyret Jul 08 '23 at 16:55
  • The code as posted results in 11 times `IndentationError: unexpected indent`. Can you [fix](https://stackoverflow.com/posts/76643724/edit) the indentation? But *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the question should appear as if it was written today) – Peter Mortensen Jul 19 '23 at 22:09

1 Answers1

1
import os
import sys

def restart_script():
    python = sys.executable
    os.execl(python, python, *sys.argv)

# Your script code here

# When you want to restart the script:
restart_script()
  • 1
    Wouldnt you have the same script, twice, after this? A webserver might refuse to start as well, seeing another pgm (its copy) is using the configured port. The *code* looks ok, but I’d be cautious about assuming it worked. – JL Peyret Jul 08 '23 at 16:46
  • I have an error: C:\Program: can't open file 'D:\\[Python Script Folder]\\Files\\Python311\\python.exe': [Errno 2] No such file or directory – KirilAf Jul 08 '23 at 17:12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 10 '23 at 23:50
  • Note that Windows does not support anything like the Unix `exec()` syscall. I assume the Python run-time is emulating that behavior by spawning a new process, waiting for it to terminate, then terminating the current process. – Kurtis Rader Jul 19 '23 at 22:40
  • @KirilAf, See my previous comment. You should avoid the solution in this answer on Windows. As JL Peyret pointed out in their comment it is better to simply kill the current process and use a process manager to restart it. – Kurtis Rader Jul 19 '23 at 22:43