Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebView widget does not load with Windows Store Python #2752

Closed
Jzhenli opened this issue Aug 8, 2024 · 15 comments · Fixed by #2778
Closed

WebView widget does not load with Windows Store Python #2752

Jzhenli opened this issue Aug 8, 2024 · 15 comments · Fixed by #2778
Labels
bug A crash or error in behavior. windows The issue relates to Microsoft Windows support.

Comments

@Jzhenli
Copy link

Jzhenli commented Aug 8, 2024

Describe the bug

Hi, I have try webview widget on win11, and it not works.

Steps to reproduce

the test code as below:

class testweb(toga.App):
    def startup(self):
        self.main_window = toga.MainWindow()

        self.webview = toga.WebView(
            on_webview_load=self.on_webview_loaded, style=Pack(flex=1)
        )
        self.url_input = toga.TextInput(
            value="https://beeware.org/", style=Pack(flex=1)
        )

        box = toga.Box(
            children=[
                toga.Box(
                    children=[
                        self.url_input,
                        toga.Button(
                            "Go",
                            on_press=self.load_page,
                            style=Pack(width=50, padding_left=5),
                        ),
                    ],
                    style=Pack(
                        direction=ROW,
                        alignment=CENTER,
                        padding=5,
                    ),
                ),
                self.webview,
            ],
            style=Pack(direction=COLUMN),
        )

        self.main_window.content = box
        self.webview.url = self.url_input.value

        # Show the main window
        self.main_window.show()

    def load_page(self, widget):
        self.webview.url = self.url_input.value

    def on_webview_loaded(self, widget):
        self.url_input.value = self.webview.url


Expected behavior

webview not display as below image:
image

Expected: the webview should display

Screenshots

No response

Environment

  • Operating System: win11 home edition 23H2
  • Python version: Python 3.10.11
  • Software versions:
    • Briefcase: 0.3.19
    • Toga:0.4.5

Logs


Additional context

No response

@Jzhenli Jzhenli added the bug A crash or error in behavior. label Aug 8, 2024
@Jzhenli Jzhenli changed the title webview widget not work on the webview widget not work on win11 Aug 8, 2024
@freakboy3742
Copy link
Member

Thanks for the report; unfortunately, I can't reproduce this problem.

If you're not receiving any other errors about the availability of the Edge webview, then my best guess is that the underlying problem is a timing error in this specific example.

If you click the "go" button, does it load the webpage? If so, then the issue could be that the webview is working, but the order of page loads is getting confused. The webview is originally being created with no initial URL; and that (empty) page is loading successfully, but because there's a load in progress, the subsequent URL change is being lost.

@freakboy3742 freakboy3742 added awaiting details More details are needed before the issue can be triaged. windows The issue relates to Microsoft Windows support. labels Aug 8, 2024
@Jzhenli
Copy link
Author

Jzhenli commented Aug 9, 2024

@freakboy3742 I add a print message in the load_page function, when click the "go" button, it will print, but no webview display.
image

@freakboy3742
Copy link
Member

This suggests the issue might be layout.

If you add a widget (say, a toga.Label) to the column box after the web view - does that widget appear in the GUI directly under the text input and button? If so, the webview is there, it's just being rendered as zero height, and therefore invisible - although I'm not sure why that would be happening.

@Jzhenli
Copy link
Author

Jzhenli commented Aug 9, 2024

@freakboy3742 I add a test label, and repeat to click the button, but the webview widget still doesn't appear in the GUI.
image

@freakboy3742
Copy link
Member

To confirm - your console log isn't showing anything errors or warnings about the web view when the app starts?

@Jzhenli
Copy link
Author

Jzhenli commented Aug 9, 2024

yes, no error in the console log @freakboy3742

image

@freakboy3742
Copy link
Member

I'm at a loss - I can't work out what is going on here.

The WebView2 assembly must be successfully loading, or you'd be getting errors in the console. I can only assume that the control isn't initialising - and isn't failing to initialise either, because if it was failing you'd be getting an error. You might be able to clarify this by putting some additional logging into Toga's winforms backend, on the WebView.winforms_initialization_completed method - this should be invoked, but the behavior you're seeing suggests it isn't.

I've done a quick search and there's some suggestions this might be related to the .NET runtime version that you have installed - what version have you got?

@Jzhenli
Copy link
Author

Jzhenli commented Aug 9, 2024

@freakboy3742 I haven't install any .NET runtime before, and now I try install the dotnet-sdk-8.0.303-win-x64, but the webview widget still not works.

@freakboy3742
Copy link
Member

I'm afraid I'm at a complete loss. As I can't reproduce your problem, it's very hard for me to investigate further. All I can suggest is that you trace through the code and try and identify what is (and isn't) happening during startup - such as tracking whether the WebView.winforms_initialization_completed method is being invoked, as I mentioned earlier. If you're able to find the source of the problem - or even a hint about where the source of the problem might lie - then we're happy to address the problem, but until we know what the problem is, there's not much we can do.

@Jzhenli
Copy link
Author

Jzhenli commented Aug 19, 2024

@freakboy3742 I have add log in the WebView.winforms_initialization_completed, it print out RuntimeError :can not find the WebView2Loader.dll

@rmartin16
Copy link
Member

@Jzhenli: The "file not found" problem triggered a thought; are you using Python from the Windows Store?

I was able to successfully recreate this behavior using the Windows Store Python:

[helloworld] Starting in dev mode...
===========================================================================
args.InitializationException=DllNotFoundException('Unable to load DLL 'WebView2Loader.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)')

@freakboy3742, does winforms_initialization_completed() run in another thread? Trying to understand where this RuntimeError is going...

@Jzhenli
Copy link
Author

Jzhenli commented Aug 19, 2024

@rmartin16 yes, I used the python from the Windows Store.

@rmartin16
Copy link
Member

@rmartin16 yes, I used the python from the Windows Store.

Thank you for the confirmation. I don't know the exact mechanism yet but Windows Store Python can do strange things with passing file paths outside of Python.

@freakboy3742, does winforms_initialization_completed() run in another thread? Trying to understand where this RuntimeError is going...

I confirmed it's the same thread. Moreover, it looks like Exceptions raised in this callback are just completely swallowed by .NET; I even tried calling sys.exit(1) and even that was swallowed. Looks like another approach will be necessary here. The MapView is similarly affected.

@rmartin16
Copy link
Member

@Jzhenli, I believe I tracked this down today; if you'd like to try out my proposed fix, you can update your pyproject.toml similar to below and run briefcase dev -r:

[tool.briefcase.app.helloworld.windows]
requires = [
    #"toga-winforms~=0.4.5",
    'git+https://github.com/rmartin16/toga@webview-for-winstore-python#subdirectory=core',
    'git+https://github.com/rmartin16/toga@webview-for-winstore-python#subdirectory=winforms',
]

@rmartin16 rmartin16 changed the title webview widget not work on win11 WebView widget does not load with Windows Store Python Aug 19, 2024
@rmartin16 rmartin16 removed the awaiting details More details are needed before the issue can be triaged. label Aug 19, 2024
@Jzhenli
Copy link
Author

Jzhenli commented Aug 20, 2024

@rmartin16 I have download the WebView2Loader.dll and register it to the windows os, now it works well. I'm not sure if your solution have fix it, because I can't reproduce this problem, but I have tried as your suggestion, and it seems works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. windows The issue relates to Microsoft Windows support.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants