posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.22k stars 69 forks source link

How can I convert "shiny for python" to ".exe" executable file. #1054

Open HotWesley opened 7 months ago

HotWesley commented 7 months ago

Because of the project, I need to convert Shiny for Python into an exe executable file.

I tried several tests using pyinstaller, but I failed. Is this possible?

It's not a Pyinstaller problem, it's an error within the shiny for python package. It looks like a bug. Below is the test I tried, can you check it??

https://stackoverflow.com/questions/77864783/how-to-convert-shiny-for-python-to-exe-file-using-pyinstaller

Below is a conversation I had with the developer of pyinstaller. Please refer to it.

https://github.com/orgs/pyinstaller/discussions/8255#discussioncomment-8235913

I'd appreciate it if you could answer me.

Have a nice day :)

wch commented 7 months ago

Interesting idea. I would disagree with the assertion, however:

sys.stderr.write(f"Error: {module_path} not found\n") is a bug in Shiny. It's assuming that a console is available which isn't true for windowed applications.

because Shiny wasn't intended for that use case.

That said, it might be worth it to try replacing sys.stderr.write() with print(..., file=sys.stderr) to see if it works with pyinstaller.

HotWesley commented 7 months ago

Thank you for your comment!

how about this. I modify main.py code as below and run .exe file (remove "from shiny._main import ...." code and add app.run())

from shiny import App, render, ui

app_ui = ui.page_fluid(
    ui.input_slider("n", "N", 0, 100, 20),
    ui.output_text_verbatim("txt"),
)

def server(input, output, session):
    @output
    @render.text
    def txt():
        return f"n*2 is {input.n() * 2}"

app = App(app_ui, server)

app.run()

Then this error has occurred.

Traceback (most recent call last):
  File "logging\config.py", line 544, in configure
  File "logging\config.py", line 656, in configure_formatter
  File "logging\config.py", line 475, in configure_custom
  File "uvicorn\logging.py", line 44, in __init__
AttributeError: 'NoneType' object has no attribute 'isatty'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main.py", line 18, in <module>
  File "shiny\_app.py", line 233, in run
    run_app(self, **kwargs)  # pyright: ignore[reportGeneralTypeIssues]
  File "shiny\_main.py", line 338, in run_app
    uvicorn.run(  # pyright: ignore[reportUnknownMemberType]
  File "uvicorn\main.py", line 522, in run
  File "uvicorn\config.py", line 276, in __init__
  File "uvicorn\config.py", line 384, in configure_logging
  File "logging\config.py", line 811, in dictConfig
  File "logging\config.py", line 547, in configure
ValueError: Unable to configure formatter 'default