That article covers all the basic stuff, right up to mentioning debugging Frappe/ERPNext server-side source code … then swerves back into basic stuff. Really great!
I need to understand a piece of deep guts ERPNext code that seems to be misbehaving:
I really did not want to do that with dozens of inserted print statements.
Here’s how I got started with Web-PDB …
pdb is the Python debugging tool.
It’s a powerful command line tool, but I was looking for something a little closer in functionality to Chrome Dev-Tools.
web-pdb does not come anywhere close to dev-tools, but it is a step up from pdb.
Installing
The first step is to add the web-pdb package to your bench environment.
Here’s the wrong way to do it.
pip install web-pdb
The the right way is to append web-pdb to the file requirements.txt which lives in the root directory of every Frappe app. You then execute: bench setup requirements.
I generally also flush the toilets with clear-cache and migrate. They seem to work like a sort of digital garlic, claimed as a panacea for all sorts of ERPNext ills, and seemingly harmless, so I just call them whenever I’ve been hacking around inside or simply things get a little blue around the gills.
erpdev@fossa:~/frappe-bench-DENH$
erpdev@fossa:~/frappe-bench-DENH$ bench --site dev.erpnext.host clear-cache
erpdev@fossa:~/frappe-bench-DENH$
erpdev@fossa:~/frappe-bench-DENH$
erpdev@fossa:~/frappe-bench-DENH$ bench --site dev.erpnext.host migrate
Migrating dev.erpnext.host
Updating DocTypes for frappe : [========================================] 100%
Updating DocTypes for erpnext : [========================================] 100%
Updating Dashboard for frappe
Updating Dashboard for erpnext
Updating customizations for Address
Updating customizations for Contact
Building search index for dev.erpnext.host
erpdev@fossa:~/frappe-bench-DENH$
erpdev@fossa:~/frappe-bench-DENH$
Calling:
The goal is to track down how and why the basic_rate value in a Stock Entry from a Work Order from a Bill of Materials is wrong. So, having decided on a likely suspect bit of code, we insert web_pdb.set_trace():
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
import json
import web_pdb # <======= YOO! HOO! It's me. Right here! See!
: : : : :
def set_basic_rate(self, reset_outgoing_rate=True, raise_error_if_no_rate=True):
"""
Set rate for outgoing, scrapped and finished items
"""
web_pdb.set_trace() # <======= ... and me again right HERE!!!
outgoing_items_cost = self.set_rate_for_outgoing_items(reset_outgoing_rate)
finished_item_qty = sum([d.transfer_qty for d in self.items if d.is_finished_item])
# Set basic rate for incoming items
for d in self.get('items'):
if d.s_warehouse or d.set_basic_rate_manually: continue
Enabling
The next step is to restart the bench. You don’t want the Supervisor running for this because you will lose visibility on a lot of valuable logging information. So do:
As an extra step, you can dig into NGinx and have it expose port 5555 as an SSL port in order to be able to access the debugger with a modern browser that automatically redirects to HTTPS. Have fun!
There’s an easier way in Chrome. Open the link:
chrome://net-internals/#hsts
Scroll right down to the bottom, stuff your server URL into the “Domain” field of “Delete domain security policies” and click on the [Delete] button
From then on, Chrome will permit plain old HTTP access to that address.
Triggering
The next step is to generate the Stock Entry by Finishing the Work Order:
When you do that, Python code on the server for the Work Order will call into the Stock Entry DocType. Part way through it will hit the web-pdb code we inserted. Your ERPNext browser page is likely to start whining and bitching about server errors and time outs, but you don’t care about that right now.
In the command line at the very bottom you can enter ad hoc Python3 commands to examine the data involved in the function you are examining. You set break points by clicking code line numbers and you can use the buttons to step through the code in various ways.
Best of all : inspect !!!
Here below you can see I can examine every attribute a one of the Item docs just by typing … inspect d
This is the most important part every ERPNext dev needs to know.
Another pretty comfortable way of debugging is to use VS Code’s debugger via SSH. You don’t even need to touch the code. Here are some docs on how to get it running.
I highly recommend this instead of changing code directly on a server and exposing your server through the browser.
@MartinHBramwell the way the VSCode’s SSH Remote feature works is that VSCode will connect to the server over SSH and it will work as if you were using VSCode on that server directly. This allows you to securely connect to the server and run whatever you want.
But honestly, I would avoid debugging on a live server in the first place. That’s just bad practice. Take a database dump and load it in a local environment which is as close to production as possible.
Our “live server” right now is a snapshotable VPS in the public cloud somewhere where we can all try things out and revert to earlier snapshots each time we think we’ve broken something.
So your advice is sound but our setup is not what you thought.