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

Feature request: Add code formatting button in toolbar for .py files #252

Open
remborg opened this issue Apr 20, 2022 · 5 comments
Open

Comments

@remborg
Copy link

remborg commented Apr 20, 2022

Hello, and thank you for creating this extension.

When opening a python file (*.py), the toolbar doesn't contain the 'code format' button.
It is possible to use the command palette to format but it's not very convenient.
It would be good to add this button for .py files, or having a config option to enable it.

@ryantam626
Copy link
Owner

Hello! Sorry for the late reply.

Of course, will try to do this in the coming days.

Though I really recommend configuring keyboard shortcuts instead of clicking a button.

@slavistan
Copy link

slavistan commented Sep 3, 2022

@ryantam626 Could you tell us how to bind a keyboard shortcut to format regular textfiles? The example settings don't work outside of notebooks. I assume that a certain selector must be set, but I can't find any documentation about those.

@ryantam626
Copy link
Owner

ryantam626 commented Sep 6, 2022

@slavistan

        {
            "args": {},
            "command": "jupyterlab_code_formatter:black",
            "keys": [
                "Ctrl K",
                "Ctrl M"
            ],
            "selector": ".jp-CodeMirrorEditor"
        },

should work.

I am not sure why jupyterlab_code_formatter:format doesn't work on textfiles just yet, hopefully this is good enough for you for now.

P.S.: Just in case you are not familiar with what "selector" means and web dev - "selector" here is CSS selector, and you can find how to target specific element inside jupyterlab with any devtools of mordern browser (e.g. Chrome DevTools), the button on the top left of Chrome DevTools is particularly useful. And also .jp-CodeMirrorEditor means any element with the jp-CodeMirrorEditor class.

@slavistan
Copy link

@ryantam626 Thanks for your helpful reply. I'll run with the black formatter only for now.

Also, I've tried concatenating the isort and black commands manually (instead of using format) as described in the docs, but only isort is ever executed. May I kindly ask you to tell me if I've made a mistake? Or is this behavior part of the problem that causes format to not function as intended?

{
  "shortcuts": [
    {
      "command": "apputils:run-all-enabled",
      "args": {
        "commands": ["jupyterlab_code_formatter:black", "jupyterlab_code_formatter:isort"],
      },
      "keys": [
        "Ctrl K",
        "Ctrl M"
      ],
      "selector": ".jp-CodeMirrorEditor"
    }
  ]
}

@remborg
Copy link
Author

remborg commented Dec 13, 2022

Hello! Sorry for the late reply.

Of course, will try to do this in the coming days.

Though I really recommend configuring keyboard shortcuts instead of clicking a button.

Hi Ryan, I was wondering if you had time to look into this?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants