python-eel / Eel

A little Python library for making simple Electron-like HTML/JS GUI apps
MIT License
6.44k stars 587 forks source link

'NoneType' object is not subscriptable in python main file #470

Closed nricciardi closed 3 years ago

nricciardi commented 3 years ago

'NoneType' object is not subscriptable NoneType error when i use expose function from javascript in python.

Code snippet(s) In settings["name"] there is a string returned from json file "settings.json" in root folder

# python file main.py
import eel
# set title for index.html based settings(.json)
@eel.expose
def change_title():
    try:
        return settings["name"]
        print("OK - change title: " + str(settings["name"]))
    except Exception as e:
        print("ERROR - change title: " + str(e))
...
// javascript file init.js
init();

async function init() {
    console.log("Initialize app (js)");
    // change title of index.html based setting.json "name"
    try {

        document.title = await eel.change_title()();
        console.log("OK - title change: " + document.title);

    } catch(err) {
        console.log("ERROR - title change: " + err.toString());
    }
}
nricciardi commented 3 years ago

You must use global

# python file main.py
import eel
# set title for index.html based settings(.json)
@eel.expose
def change_title():
    try:
        global settings
        return settings["name"]
        print("OK - change title: " + str(settings["name"]))
    except Exception as e:
        print("ERROR - change title: " + str(e))