Coder Social home page Coder Social logo

mlucool / jupyterlab_code_formatter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryantam626/jupyterlab_code_formatter

0.0 2.0 0.0 10.42 MB

A universal code formatter for JupyterLab.

License: MIT License

Python 46.50% TypeScript 38.44% Jupyter Notebook 15.06%

jupyterlab_code_formatter's Introduction

jupyterlab_code_formatter

Binder npm version npm downloads code style

Code formatter for JupyterLab.

Prerequisites

  • JupyterLab installed under Python3.6+ environment

Why Python3.6+

  • Mainly because Black requires 3.6+, and I like using Black the most;
  • Typing support requires 3.5+, and I like typing;

Installation

Using pip

jupyter labextension install @ryantam626/jupyterlab_code_formatter
pip install jupyterlab_code_formatter
jupyter serverextension enable --py jupyterlab_code_formatter
# Remember to install one of the supported formatters (it's in the next section)

Using conda

conda install -c conda-forge black
jupyter labextension install @ryantam626/jupyterlab_code_formatter
conda install -c conda-forge jupyterlab_code_formatter
jupyter serverextension enable --py jupyterlab_code_formatter

Make sure to restart jupyter server after installation.

Installation of formatters

For jupyterlab_code_formatter to work, you would also need some formatters, three are currently supported:

Python

  • Black;
  • YAPF;
  • Autopep8;
  • Isort; Install at least one of them through pip install black for example (or its equivalent in other package management systems).

R

  • formatR
    • using pip: pip install rpy2 and use R command to install formatR package
    • using conda: conda install rpy2 r-formatr
  • styler
    • using pip: pip install rpy2 and use R command to install styler package
    • using conda: conda install rpy2 r-styler

Usage

Assuming you do have one of the formatters installed in the Python environment that runs JupyterLab, some extra options would appear under JUPYTERLAB CODE FORMATTER, use those to apply formatting to current codecell (no multiple code cell application atm).

Configs

There are some formatter config exposed through the Jupyter Lab Advanced Settings Editor, have a browse and change it if you wish. for example:

{
    "autopep8": {
        "max_line_length": 120,
        "ignore": [
            "E226",
            "E302",
            "E41"
        ]
    }
}

How about a keyboard shortcut?

Assuming you are using jupyterlab>=1.0.0, you can add an extra entry to your keyboard shortcuts settings with something like

{
    "shortcuts": [
        {
            "command": "jupyterlab_code_formatter:black",
            "keys": [
                "Ctrl K",
                "Ctrl M"
            ],
            "selector": ".jp-Notebook.jp-mod-editMode"
        }
    ]
}

This basically says "Under edit mode (detected through the selector), using the chord Ctrl K + Ctrl M, invoke the jupyterlab_code_formatter:black command". And there you have it ๐ŸŽ‰

For Autopep8, YAPF or Isort, simply do autopep8/yapf/isort instead of black.

Using the Black formatter on Windows

You might see some errors about writing grammar tables when using the black formatter on Windows, simply manually create the directory as suggested in here and you should be good to go.

Why are the commands not showing up in the command pallete?

Make sure you really have one of the formatters properly installed.

And also make sure you have a suitable notebook/script opened for the formatters to work on when checking if the commands are there.

Development

For a development install (requires npm version 4 or later), do the following in the repository directory:

npm install
npm run build
jupyter labextension link .
pip install -e .

To rebuild the package and the JupyterLab app:

npm run build
jupyter lab build

jupyterlab_code_formatter's People

Contributors

akashlakhera avatar caselit avatar dev-wei avatar gnestor avatar jtpio avatar mzakariacern avatar rbedi avatar reza1615 avatar ryantam626 avatar timlod avatar umangs94 avatar xhochy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.