Pywebview code problem, need help pls

Hi, this is the first time I’m using pywebview and I need your help. Unfortunately I’m having a few problems when I try to use it. With my code I try to retrieve a token and a specific ID when I connect to a website, but when my program ends I get ‘None’ instead of a list of numbers and letters for the token and ID.
Here’s my code:

    def web_view(token, ide):
            
    token, ide = None, None
    window = webview.create_window('Simple browser', url, confirm_close=False)
            
    def on_loaded():        
        current_url = window.get_current_url()        
        if "https://m.facebook.com/v12.0/dialog/oauth/confirm/?paipv=" in current_url:
            page_source = window.evaluate_js('document.documentElement.outerHTML')
            token = extract_access_token(page_source)
            ide = extract_id_token(page_source)
            if token:
                print('Access Token:', token)
            if ide:
                print('ID Token:', ide)
            if token is not None and ide is not None:
                window.destroy()                
     
    def extract_access_token(html):
        pattern = r'access_token=([^&]+)'
        match = re.search(pattern, html)
        if match:
            return match.group(1)
        return None
    def extract_id_token(html):
        pattern = r'id_token=([^&]+)'
        match = re.search(pattern, html)
        if match:
            return match.group(1)
        return None      
       
    window.events.loaded += on_loaded

    webview.start()
    return token, ide

To be more precise, when I get to the “on_loaded” part, the end of this part gives me the correct ID and token, and it closes the window properly once I’ve been given this information. However, once the window closes, my program returns ‘None’ for the ID and token, whereas just before it had correctly displayed the values.

I would be very grateful if you could help me with this.

In a function, when you assign to a name, Python assumes that it’s local to that function unless you declare it nonlocal or global. nonlocal means that it exists in the parent (nested) function and global means that it exists in the main code of the module (file).

In on_loaded, you’re assigning to token and ide, so they are assumed to be local to on_loaded. If you want it to assign to the token and ide of the parent function web_view, you should declare them as nonlocal in on_loaded.

First of all, thank you for your answers. Unfortunately I still haven’t been able to solve my problem even after modifying my code.

In fact, my web_view() function is used in another function to retrieve my token and id. Here is the code:

def linking():    
   web_view()
   global token
   global ide
   print('test 2', token, ide)    
   if token is not None:
       try:
           .........
   else:
       print('{error}cannot fetch token.')




def web_view():
           
   token, ide = None, None
   window = webview.create_window('Simple browser', url, confirm_close=False)
           
   def on_loaded():        
       current_url = window.get_current_url()        
       if "https://m.facebook.com/v12.0/dialog/oauth/confirm/?paipv=" in current_url:
           page_source = window.evaluate_js('document.documentElement.outerHTML')
           nonlocal token
           nonlocal id
           token = extract_access_token(page_source)
           ide = extract_id_token(page_source)
           if token:
               print('Access Token:', token)
           if ide:
               print('ID Token:', ide)
           if token is not None and ide is not None:
               window.destroy()                
    
   def extract_access_token(html):
       pattern = r'access_token=([^&]+)'
       match = re.search(pattern, html)
       if match:
           return match.group(1)
       return None
   def extract_id_token(html):
       pattern = r'id_token=([^&]+)'
       match = re.search(pattern, html)
       if match:
           return match.group(1)
       return None      
      
   window.events.loaded += on_loaded

   webview.start()
   print('test 1 : ', token, ide)
   return token, ide

I put the nonlocal token and ide as you advised in the on_loaded() function.
Unfortunately my problem is still the same, when my program executes print('test 1:') I get the right token and the right ide but when I exit the web_view() function to continue the linking function print('test 2 :') gives me ‘None’ for the token and the ide.
I don’t see what more I can do to resolve my code.

There are multiple conceptual issues here and it will be better if you study this first:

First of all, you wrote nonlocal id instead of nonlocal ide in what you posted.

In on_loaded, you told it that token and ide are nonlocal, so now they exist in the parent function web_view.

linking won’t see them because, well, they’re local to web_view.

You wrote global token and global ide in linking, so it’s going to look for token and ide as global names, but those names nothing to do with the ones that are local to web_view. They happen to be named the same, but that’s just a coiincidence - they are completely separate.