Skip to content

Commit 8d4cb58

Browse files
committed
Update v3/docs/developer-overview.md
1 parent 93bbe9c commit 8d4cb58

1 file changed

Lines changed: 64 additions & 49 deletions

File tree

‎v3/docs/developer-overview.md‎

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ elapsed since that date, the more likely things are out-of-date.
1010

1111
I'm assuming that you're competent in Python, JavaScript, command-line-fu, and Google-fu,
1212
so I won't do much hand-holding in these directions.
13-
But feel free to email philip@pgbovine.net if you have questions.
13+
14+
This guy isn't meant to be comprehensive; rather, it's just a starting point for learning about the code
15+
and development workflow. You will undoubtedly still be confused about details after reading it, so
16+
feel free to email philip@pgbovine.net if you have questions.
1417

1518
And please excuse the sloppy writing; I'm not trying to win any style awards here :)
1619

@@ -36,7 +39,7 @@ btw, using the default configuration, http://localhost:8080/ is actually loading
3639
(See `v3/pythontutor.py` for details.)
3740

3841
The benefit of running OPT locally is that you can test all changes without going online. So even
39-
if you're eventually going to deploy NOT on Google App Engine, it makes good sense to install locally
42+
if you're eventually going to deploy on something other than Google App Engine, it still makes sense to install locally
4043
for development and testing. The main caveat here is that Google App Engine currently runs Python 2.7,
4144
so you won't be able to test Python 3 locally this way.
4245

@@ -46,45 +49,49 @@ so you won't be able to test Python 3 locally this way.
4649
OPT consists of a pure-Python backend and an HTML/CSS/JavaScript frontend.
4750
Here is a typical user interaction sequence:
4851

49-
1. The user types in Python code in the web-based text editor (powered by [CodeMirror](http://www.codemirror.net/)).
52+
1. The user visits [visualize.html](http://pythontutor.com/visualize.html) and types in Python code in the web-based text editor.
5053
2. The user hits the "Visualize execution" button.
5154
3. The OPT frontend sends the user's Python code as a string to the backend by making an AJAX GET request.
52-
4. The backend executes the Python code under the supervision of the Python [bdb](http://docs.python.org/library/bdb.html) debugger module, produces an execution trace, and send that trace back to the frontend in JSON format.
53-
5. The frontend switches to a visualization pane, parses the execution trace, and renders the appropriate stack frames and heap objects.
54-
6. When the user interacts with the frontend by stepping through execution points (forwards and backwards), the frontend renders the proper data structures WITHOUT making another call to the backend.
55+
4. The backend executes the Python code under the supervision of the Python [bdb](http://docs.python.org/library/bdb.html) debugger, produces an execution trace, and send that trace back to the frontend in JSON format.
56+
5. The frontend switches to a visualization display, parses the execution trace, and renders the appropriate stack frames, heap objects, and pointers.
57+
6. When the user interacts with the frontend by stepping through execution points (forwards and backwards), the frontend renders the proper data structures **without** making another subsequent call to the backend.
5558

5659
All relevant files are located in `OnlinePythonTutor/v3/`, since v3 is the currently-active version.
5760

5861
The frontend consists of:
59-
visualize.html
60-
css/opt-frontend.css
61-
js/opt-frontend.js
62-
css/pytutor.css
63-
js/pytutor.js
64-
<a bunch of auxiliary css and js files such as libraries>
62+
```
63+
visualize.html
64+
css/opt-frontend.css
65+
js/opt-frontend.js
66+
css/pytutor.css
67+
js/pytutor.js
68+
<a bunch of auxiliary css and js files such as libraries>
69+
```
6570

66-
`pytutor.[js|css]` contain the bulk of the OPT frontend code. In theory, if you set things up correctly,
67-
you should be able to **embed** an OPT visualization into any webpage with one line of JavaScript that looks like:
71+
`pytutor.[js|css]` contain the bulk of the OPT frontend code. In theory, you should be able to **embed** an
72+
OPT visualization into any webpage with one line of JavaScript that looks like:
6873

6974
```javascript
7075
var v = new ExecutionVisualizer(domRoot, traceFromBackend, optionalParams);
7176
```
7277

7378
Thus, the design of `pytutor.[js|css]` is meant to be as modular as possible, which means abstracting
74-
everything in an `ExecutionVisualizer` object. This way, you can create multiple visualizer objects
75-
to embed on the same webpage and not have them interfere with one another.
79+
everything in an `ExecutionVisualizer` class. This way, you can create multiple visualizer objects
80+
to embed on the same webpage without them interfering with one another.
7681

7782
`opt-frontend.[js|css]` contain code that is specific to the `visualize.html` page and doesn't make sense for, say,
78-
embedding OPT visualizations into other types of webpages.
83+
embedding OPT visualizations into other webpages.
7984

8085
The backend consists of:
86+
```
87+
pg_logger.py - the main entry point to the OPT backend
88+
pg_encoder.py - encodes the trace format into JSON to send to frontend
89+
generate_json_trace.py - script to test the backend independent of the frontend
90+
app.yaml, pythontutor.py - files for deploying on Google App Engine
91+
web_exec.py - example CGI script for deploying backend on CGI-enabled webservers
92+
```
8193

82-
pg_logger.py : the main entry point for the OPT backend
83-
pg_encoder.py : encodes the trace format into JSON to send to frontend
84-
generate_json_trace.py : script to test the backend independent of the frontend
85-
app.yaml and pythontutor.py : config files for Google App Engine
86-
web_exec.py : example CGI script for deploying backend on CGI-enabled webservers
87-
94+
The backend works with both Python 2 and 3.
8895

8996
## Hacking on the backend
9097

@@ -93,15 +100,21 @@ To modify the backend, you will mainly need to understand `pg_logger.py` and `pg
93100

94101
### Two quick tips for starters
95102

103+
Since the backend's details might change, rather than documenting every last detail, I'd rather equip you with
104+
the knowledge needed to experiment with the code yourself, since that knowledge is less likely to get outdated.
105+
96106
First, run `generate_json_trace.py` to see the trace that the backend generates for a given input Python program.
97-
This is the main way to do an "end-to-end" test on your backend modifications. For example, if you wrote a Python
98-
program stored in `example.py`, then running:
107+
This is the main way to do an "end-to-end" test on your backend modifications. For example, if you want the backend
108+
to process a Python program stored in `example.py`, then run:
99109

100-
python generate_json_trace.py example.py
101-
102-
will print a JSON-formatted execution trace to stdout. This is exactly what the backend sends to the frontend.
110+
```
111+
python generate_json_trace.py example.py
112+
```
113+
114+
Doing so will print a JSON-formatted execution trace to stdout.
115+
This data is exactly what the backend sends to the frontend.
103116
(Actually not quite: the sent trace is actually compressed to eliminate all extraneous spaces and newlines.
104-
But for testing, I've made the trace more human-readable.)
117+
But for testing purposes, I've made the trace more human-readable.)
105118

106119
Second, when you're "print debugging" in the backend, you can't simply print to stdout, since `pg_logger.py`
107120
*redirects* stdout to a buffer. Instead, you need to write all of your print statements as:
@@ -112,25 +125,26 @@ print >> sys.stderr, <debug message to print>
112125
113126
so that the output goes to stderr.
114127

115-
The easiest way to debug is to insert in print statements (to stderr) and then run `generate_json_trace.py` on
116-
small code examples.
128+
The easiest way to debug or investigate how some part of the code works is to **insert in print statements (to stderr)
129+
and then run `generate_json_trace.py` on small code examples**. Trust me -- being able to do this is way more
130+
effective than memorizing detailed documentation (which could be outdated by the time you read it!)
117131

118132

119133
### Backend control flow
120134

121-
Let's trace through a typical backend run to get a sense of how it works:
135+
Let's now trace through a typical backend run to get a sense of how it works.
122136

123137
The main entry point is this function in `pg_logger.py`:
124138

125139
```python
126140
def exec_script_str(script_str, cumulative_mode, finalizer_func):
127141
```
128142

129-
`script_str` is the entire string contents of the Python program for the backend to execute.
143+
`script_str` contains the entire string contents of the Python program for the backend to execute.
130144
Ignore `cumulative_mode` for now (just set it to `False`). `finalizer_func` is the function to call
131145
after the backend is done generating a trace.
132146

133-
Let's look at how `generate_json_trace.py` calls `exec_script_str` (using a simplified version of the code):
147+
Let's look at how `generate_json_trace.py` calls `exec_script_str` (using a simplified version of its code):
134148

135149
```python
136150
import pg_logger, json
@@ -143,7 +157,7 @@ def json_finalizer(input_code, output_trace):
143157
pg_logger.exec_script_str(open("example.py").read(), False, json_finalizer)
144158
```
145159

146-
In this simplified example, the script opens `example.py`, reads its contents into a string, and passes it
160+
In this simplified example, the script opens `example.py`, reads its contents into a string, and passes that string
147161
into `exec_script_str`. The finalizer function is `json_finalizer`, which takes two parameters --
148162
the original code from `example.py` (`input_code`) and the execution trace that it produced (`output_trace`) --
149163
inserts both into a dict, encodes that dict as a JSON object, and then prints that JSON object to stdout.
@@ -152,13 +166,13 @@ That's why when you run `generate_json_trace.py`, its output is a JSON object pr
152166
Note that if you pass in another finalizer function, then you can do other actions like postprocessing
153167
the output trace or saving it to a file rather than printing to stdout.
154168

155-
Now that you know what you pass into `exec_script_str` and what comes out of it, let's dive into its guts
156-
to see how that execution trace (`output_trace`) is produced. Here is the code for `exec_script_str` in `pg_logger.py`:
169+
Now that you know what's passed into `exec_script_str` and what comes out of it, let's dive into its guts
170+
to see how that all-important execution trace (`output_trace`) is produced.
171+
Here is the code for `exec_script_str` in `pg_logger.py`:
157172

158173
```python
159174
def exec_script_str(script_str, cumulative_mode, finalizer_func):
160175
logger = PGLogger(cumulative_mode, finalizer_func)
161-
162176
try:
163177
logger._runscript(script_str)
164178
except bdb.BdbQuit:
@@ -167,26 +181,27 @@ def exec_script_str(script_str, cumulative_mode, finalizer_func):
167181
logger.finalize()
168182
```
169183

170-
This code creates a `PGLogger` object then calls its `_runscript` method to run the script passed in as `script_str`.
184+
This code creates a `PGLogger` object then calls its `_runscript` method to run the user's program (from `example.py`),
185+
which is passed in as `script_str`.
171186
After execution finishes (possibly due to a bdb-related exception), the `finalize` method is run. This method
172-
does some postprocessing of the trace (`self.trace`) and then calls the user-supplied `finalizer_func`.
187+
does some postprocessing of the trace (`self.trace`) and then finally calls the user-supplied `finalizer_func`.
173188

174189
`PGLogger` is a subclass of [bdb.Bdb](http://docs.python.org/library/bdb.html#bdb.Bdb),
175-
which is the Python standard debugger module. It stores a ton of fields to record what
176-
is going on as the target program (that the user passed in) executes. Its `_runscript` method
190+
which is the Python standard debugger module. It stores lots of fields to record what
191+
is going on as it executes the program that the user passed in as `script_str`. Its `_runscript` method
177192
is where the action starts. This method first sets up a sandboxed environment containing a restricted
178-
set of builtins (`user_builtins`) and redirection for stdout (`user_stdout`), and then executes:
193+
set of builtins (`user_builtins`) and redirection for stdout (`user_stdout`), and then executes this code:
179194

180195
```python
181-
try:
182-
self.run(script_str, user_globals, user_globals)
183-
except SystemExit:
184-
raise bdb.BdbQuit
196+
try:
197+
self.run(script_str, user_globals, user_globals)
198+
except SystemExit:
199+
raise bdb.BdbQuit
185200
```
186201

187202
The `run` method is actually [inherited from bdb.Bdb](http://docs.python.org/library/bdb.html#bdb.Bdb.run).
188-
It executes the script in a modified global environment (`user_globals`).
203+
It executes the contents of `script_str` in a modified global environment (`user_globals`).
189204

190-
Ok, the debugger has just started executing the script that the user has passed in (from `example.py` in our example).
205+
Ok, the debugger has just started executing the script that the user passed in (from `example.py` in our example).
191206
What happens now?
192207

0 commit comments

Comments
 (0)