Easy, free to use online formatter for Python code with configuration options?
Is there something like prettier for python?
Videos
I am searching for a formatter for Python3 which takes a few simple styling parameters and yet is very easy to use. All I found so far where tons of formatters, which ansolutely enforce one and only one formatting style.
Example input:
def deleteSubnet(ipAddress, cidrMask, interface = defaultNetworkInterface):
if subprocess.run(["ip","a","d",ipAddress+"/"+cidrMask,"dev",interface], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode != 0:
quit(color.red+" ERROR:"+color.stop+" IP could not be cleared. You need to manually remove "+color.blue+ipAddress+"/"+cidrMask+color.stop+" from "+color.blue+interface+color.stop+"!!! Terminating...")
print(color.green+" Success: deleted IP "+color.stopcolor.blue+ipAddress+"/"+cidrMask+color.stop+" from interface "+color.blue+interface+color.stop+".")Example output (NOT WANTED):
def deleteSubnet(ipAddress, cidrMask, interface=defaultNetworkInterface):
if (
subprocess.run(
["ip", "a", "d", ipAddress + "/" + cidrMask, "dev", interface]
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode
!= 0
):
quit(
color.red
+ " ERROR:"
+ color.stop
+ " IP could not be cleared. You need to manually remove "
+ color.blue
+ ipAddress
+ "/"
+ cidrMask
+ color.stop
+ " from "
+ color.blue
+ interface
+ color.stop
+ "!!! Terminating..."
)
print(
color.green
+ " Success: deleted IP "
+ color.stopcolor.blue
+ ipAddress
+ "/"
+ cidrMask
+ color.stop
+ " from interface "
+ color.blue
+ interface
+ color.stop
+ "."
)Yes it is ugly. Does not matter. If reddit does word-wrap the lines, it will be horrible. But if the line just overflows, it just looks great for me.
All I want to do is a simple tool that understands the indentations and allows me to configure a target indentation. I do not care for the standard that tools like pylint try to enforce - i dislike it.
However what is way worse than not breaking lines, is uneven indentations. And that I need to fix - but please not manually. Is there an easy to use tool, that can fix my *variable* indentations to exactly 2 spaces per indent without changing the rest of the content of the lines? (yes I want 2 spaces, period)
For javascript there are amazing tools, like beautifier.io which handles it perfectly. Why can't I find something like that for python? And yes, I prefer a simple online tool actually, that does not require a login or download.
As to why it is uneven: It is a copy&paste collection from different sources.
Thanks for your help.
I just want to reformat everything so it's perfect. Is there anything like that in Python? What's the best?
If I disabled Prettier as the default formatter, it would not format on save anymore, but my Python would be formatted by autopep8 on save. With this in mind, the following solution worked for me to have both Prettier working for other languages and autopep8 for Python:
Copy{
"workbench.iconTheme": "vscode-icons",
"workbench.editorAssociations": [
{
"viewType": "jupyter.notebook.ipynb",
"filenamePattern": "*.ipynb"
}
],
"git.confirmSync": false,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"python.formatting.provider": "autopep8",
"explorer.confirmDelete": false,
"python.showStartPage": false,
"explorer.confirmDragAndDrop": false,
"python.linting.pylintArgs": ["--load-plugins=pylint_django"],
"javascript.updateImportsOnFileMove.enabled": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[python]": {
"editor.defaultFormatter": "ms-python.python"
}
}
Let me know if somebody finds a better solution!
Meaningful config snippet from @round_circle's answer:
Copy"[python]": {
"editor.defaultFormatter": "ms-python.python"
}
After adding it, autopep8 worked for python files.