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

C/C++ SettingsStore deleted #166

Open
rblumenowgs opened this issue Aug 26, 2021 · 10 comments
Open

C/C++ SettingsStore deleted #166

rblumenowgs opened this issue Aug 26, 2021 · 10 comments
Labels
bug Something isn't working

Comments

@rblumenowgs
Copy link

Happy to pay to get this bug fixed/fast-tracked :)

When opening PandasGUI, editing data, closing, and attempting to reopen, I get the following error:

RuntimeError: wrapped C/C++ object of type SettingsStore has been deleted
Traceback:
File "c:\users\ryan\appdata\local\programs\python\python39\lib\site-packages\streamlit\script_runner.py", line 350, in _run_script
exec(code, module.dict)
File "D:\Desktop\Jobs\Globalstratos\Frontend\GlobalstratosUI\app.py", line 42, in
dashboard.run()
File "D:\Desktop\Jobs\Globalstratos\Frontend\GlobalstratosUI\multipage2.py", line 138, in run
page'function'
File "D:\Desktop\Jobs\Globalstratos\Frontend\GlobalstratosUI\DBsiteinfo.py", line 339, in siteinfo
gui = show(sites_transposed = sites_transposed, settings={'block':True})
File "c:\users\ryan\appdata\local\programs\python\python39\lib\site-packages\pandasgui\gui.py", line 460, in show
pandas_gui = PandasGui(settings=settings, **kwargs)
File "c:\users\ryan\appdata\local\programs\python\python39\lib\site-packages\pandasgui\gui.py", line 73, in init
self.init_ui()
File "c:\users\ryan\appdata\local\programs\python\python39\lib\site-packages\pandasgui\gui.py", line 161, in init_ui
self.store.settings.settingsChanged.connect(self.apply_settings)

@rblumenowgs rblumenowgs added the bug Something isn't working label Aug 26, 2021
@adamerose
Copy link
Owner

I've been unable to consistently reproduce this error so far, let me know if you can create a minimum reproducible example

This snippet runs with no errors:

from pandasgui import show
from pandasgui.datasets import pokemon

gui = show(pokemon, settings={'block':True})
# *Now I edit data and close the first GUI, then the 2nd one opens*
gui2 = show(pokemon, settings={'block':True})

@rblumenowgs
Copy link
Author

rblumenowgs commented Aug 27, 2021 via email

@rblumenowgs
Copy link
Author

rblumenowgs commented Aug 27, 2021 via email

@adamerose
Copy link
Owner

adamerose commented Aug 27, 2021

The bug happens for me when I edit the data, then close the first GUI, and then try and reopen PandasGUI again.

This is what my snippet above does though, so there must be some other factor at play here (my snippet runs without error for me let me know if different on your end). We can set up a call in DMs but in the mean time try making a minimal example (throw the error using a sample dataset and include as little other code as possible)

@rblumenowgs
Copy link
Author

rblumenowgs commented Aug 29, 2021

Hi Adam,

Here we go :) Please run the following in a standalone file, I run it from within VS Code. Then click the button it brings up, close PandasGUI, and click the button again. This triggers the error I get from my side.

import streamlit
import streamlit as st
from pandasgui import show
import sys
from streamlit import cli as stcli
import pandas as pd


def mainprgm():
    data = pd.DataFrame([1, 2, 3, 4, 5])

    datagui = st.button("Click for data")

    if datagui == True:
        gui = show(data)


if __name__ == '__main__':
    if streamlit._is_running_with_streamlit:
        mainprgm()
    else:
        sys.argv = ["streamlit", "run", sys.argv[0]]
        sys.exit(stcli.main())

@adamerose
Copy link
Owner

This is a tricky bug related to running PyQt5 outside the main thread, which is what streamlit does. When I run your code on my machine it actually crashes completely before even showing a stack trace. If I modify yours to manually create a QApplication before mainprgm I get your error but it's not easily solvable it's basically saying C++ have been destroyed or are not available because they're only accessible from the main thread.

I'm not sure if it's possible to fix this... I'd need to have a method when called in a non-main thread to trigger a method to run in the main threads. Maybe there's some workaround to do that but not sure yet

@rblumenowgs
Copy link
Author

rblumenowgs commented Aug 30, 2021 via email

@ecyoung3
Copy link

ecyoung3 commented Nov 8, 2021

I managed to fix this by implementing a copy() method for SettingsStore that creates a new instance with all the same settings. I couldn't come up with a good way of transferring ownership of the SettingsStore (QObject) to the PandasGui, but simply creating a new instance seems to work. Implementation of SettingsStore.copy() method below:

def copy(self):
        """ 
        Create a copy of the settings with a new QObject. Intended as a workaround
        to this bug: https://github.com/adamerose/PandasGUI/issues/166
        """
        # Create new settings instance
        new_settings = SettingsStore()
        
        # Copy every attribute that's a Setting
        for attr, value in self.__dict__.items():
            if isinstance(value, Setting):
                setattr(new_settings, attr, value)
                
        return new_settings

I am not using streamlit but rather Spyder (version 5.0) and was encountering the same bug until implementing the above workaround. I added this at the end of the PandasGui init_ui definition:

        # Create a copy of the settings in case the SettingsStore reference has
        # been discarded by Qt prematurely
        # https://stackoverflow.com/a/17935694/10342097
        self.store.settings = self.store.settings.copy()

        # Signals
        self.store.settings.settingsChanged.connect(self.apply_settings)

        self.apply_settings()

@PhilipDeegan
Copy link

@ecyoung3 thanks!

@chadkennedyonline
Copy link

Should @ecyoung3's fix be added in via a PR?

slabko added a commit to slabko/PandasGUI that referenced this issue Jan 17, 2022
adamerose added a commit that referenced this issue Jan 17, 2022
Fix issue #166 as proposed by ecyoung3
adamerose pushed a commit that referenced this issue Feb 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants
@PhilipDeegan @adamerose @chadkennedyonline @ecyoung3 @rblumenowgs and others