reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Debug in Pycharm

Open nicornk opened this issue 2 years ago β€’ 4 comments

Hi Team,

I would like to run pynecone from a Pycharm Run Configuration to be able to set breakpoints in the code.

Did anyone get this working? I tried adding the following lines to my pynecone sample app:

if __name__ == '__main__':
    from pynecone.pc import main

    main(["run"])

However, running that fails with

──────────────────────────── Starting Pynecone App ─────────────────────────────
Traceback (most recent call last):
  File "/Users/Username/Git/pynecone-test/pynecone_test/pynecone_test.py", line 296, in <module>
    main(["run"])
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/typer/main.py", line 214, in __call__
    return get_command(self)(*args, **kwargs)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/typer/main.py", line 532, in wrapper
    return callback(**use_params)  # type: ignore
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/pynecone/pc.py", line 104, in run
    app = utils.get_app()
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/pynecone/utils.py", line 373, in get_app
    return __import__(module, fromlist=(constants.APP_VAR,))
  File "/Users/Username/Git/pynecone-test/pynecone_test/pynecone_test.py", line 8, in <module>
    class Customer(pc.Model, table=True):
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlmodel/main.py", line 369, in __init__
    DeclarativeMeta.__init__(cls, classname, bases, dict_used, **kw)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlalchemy/orm/decl_api.py", line 76, in __init__
    _as_declarative(reg, cls, dict_)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 126, in _as_declarative
    return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 183, in setup_mapping
    return cfg_cls(registry, cls_, dict_, table, mapper_kw)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 331, in __init__
    self._setup_table(table)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 854, in _setup_table
    table_cls(
  File "<string>", line 2, in __new__
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
    return fn(*args, **kwargs)
  File "/usr/local/Caskroom/mambaforge/base/envs/pynecone/lib/python3.10/site-packages/sqlalchemy/sql/schema.py", line 594, in __new__
    raise exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Table 'customer' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

nicornk avatar Mar 14 '23 12:03 nicornk

Hi, I do it this way and it works for me, I hope it can be useful for others as well. Clone this repository, don't use pip version git clone https://github.com/pynecone-io/pynecone Navigate to the folder cd pynecone just cloned Setup a virtual env (PyCharm will do it for you) Run pip install . Now create a new Python configuration from PyCharm Now set Script path with pc.py path Parameters: run Working directory: folder of your current pynecone project. See this example for quick overview

Example

HellAmbro avatar Mar 14 '23 13:03 HellAmbro

If somebody debugs on vscode, the launch.json looks like below: the cwd is the working directory.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "pc",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/pynecone/pc.py",
            "console": "integratedTerminal",
            "justMyCode": true,
            "args": ["run"],
            "cwd": "${workspaceFolder}/examples/hello_world"
        }
    ]
}

d29107d avatar Mar 15 '23 17:03 d29107d

Even better is using the module...this should be able to be translated to either pycharm or vscode or whatever debugger you want to utilize.

{
            "name": "pynecone gui",
            "type": "python",
            "request": "launch",
            "module": "pynecone.pc",
            "justMyCode": true,
            "args": [
                "run"
            ],
            "cwd": "${workspaceFolder}/gui"
        }

craig8 avatar Jun 21 '23 20:06 craig8

Similar to #1184

masenf avatar Jul 07 '23 17:07 masenf

Now set Script path with pc.py path Parameters: run Working directory: folder of your current pynecone project. See this example for quick overview

For me, this allowed running, but not debugging and hitting breakpoints. (In Pycharm)

allComputableThings avatar Jul 15 '23 00:07 allComputableThings

Hi everyone,

Having a working debugging environment in PyCharm would indeed improve the developer experience. Has anyone found a workaround with the new Reflex naming? I can get PyCharm to stop at the breakpoints on initial rendering, but not with subsequent reloads.

DFranch avatar Jul 24 '23 13:07 DFranch

image

https://www.youtube.com/watch?v=KQShed3LA64&pp=ygUOcHljaGFybSByZWZsZXg%3D

masenf avatar Nov 27 '23 19:11 masenf