# Pywebview code problem, need help pls

**URL:** <https://discuss.python.org/t/pywebview-code-problem-need-help-pls/31091>\
**Category:** Python Help\
**Tags:** help\
**Created:** [August 5, 2023, 9:45pm UTC](https://discuss.python.org/t/pywebview-code-problem-need-help-pls/31091 "2023-08-05T21:45:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrad](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Henrad](https://discuss.python.org/u/Henrad)\
**Post date:** [August 5, 2023, 9:45pm UTC](https://discuss.python.org/t/pywebview-code-problem-need-help-pls/31091/1 "2023-08-05T21:45:50Z")

</div>

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:

```python
    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.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [August 6, 2023, 12:17am UTC](https://discuss.python.org/t/pywebview-code-problem-need-help-pls/31091/2 "2023-08-06T00:17:00Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Henrad](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Henrad](https://discuss.python.org/u/Henrad)\
**Post date:** [August 6, 2023, 10:03am UTC](https://discuss.python.org/t/pywebview-code-problem-need-help-pls/31091/4 "2023-08-06T10:03:40Z")

</div>

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:

```python
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.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [August 6, 2023, 12:59pm UTC](https://discuss.python.org/t/pywebview-code-problem-need-help-pls/31091/5 "2023-08-06T12:59:35Z")

</div>

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

> <https://stackoverflow.com/questions/3052793/how-do-i-get-a-result-output-from-a-function-how-can-i-use-the-result-later>

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [August 6, 2023, 4:25pm UTC](https://discuss.python.org/t/pywebview-code-problem-need-help-pls/31091/6 "2023-08-06T16:25:05Z")

</div>

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.
