Skip to content

Commit df0eb7e

Browse files
committed
Update v3/docs/developer-overview.md
1 parent eda1672 commit df0eb7e

1 file changed

Lines changed: 73 additions & 8 deletions

File tree

‎v3/docs/developer-overview.md‎

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@ Online Python Tutor (thereafter abbreviated as OPT). View it online at:
66
https://github.com/pgbovine/OnlinePythonTutor/blob/master/v3/docs/developer-overview.md
77

88
Look at the Git history to see when this document was last updated; the more time
9-
elapsed since that date, the more likely things are out-of-date. Please email
10-
philip@pgbovine.net if you have questions.
9+
elapsed since that date, the more likely things are out-of-date.
10+
11+
I'm assuming that you're competent in Python, JavaScript, command-line-fu, and Google-fu,
12+
so I won't do much hand-holding in these directions.
13+
But feel free to email philip@pgbovine.net if you have questions.
1114

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

1417

15-
## Running OPT locally on your machine
18+
## Getting Started: Running OPT locally on your machine
19+
20+
When you check out OPT from GitHub, it's configured by default to run on Google App Engine
21+
(but it can also run fine on a CGI-enabled webserver such as Apache).
1622

17-
When you check out OPT from GitHub, it's configured by default to run on
18-
Google App Engine. So download the Google App Engine [Python SDK](https://developers.google.com/appengine/downloads)
23+
To run a local instance, download/install
24+
the Google App Engine [Python SDK](https://developers.google.com/appengine/downloads)
1925
for your OS and then run:
2026

2127
cd OnlinePythonTutor/
@@ -60,7 +66,9 @@ The frontend consists of:
6066
`pytutor.[js|css]` contain the bulk of the OPT frontend code. In theory, if you set things up correctly,
6167
you should be able to **embed** an OPT visualization into any webpage with one line of JavaScript that looks like:
6268

63-
var v = new ExecutionVisualizer(domRoot, traceFromBackend, optionalParams);
69+
```javascript
70+
var v = new ExecutionVisualizer(domRoot, traceFromBackend, optionalParams);
71+
```
6472

6573
Thus, the design of `pytutor.[js|css]` is meant to be as modular as possible, which means abstracting
6674
everything in an `ExecutionVisualizer` object. This way, you can create multiple visualizer objects
@@ -82,6 +90,7 @@ The backend consists of:
8290

8391
To modify the backend, you will mainly need to understand `pg_logger.py` and `pg_encoder.py`.
8492

93+
8594
### Two quick tips for starters
8695

8796
First, run `generate_json_trace.py` to see the trace that the backend generates for a given input Python program.
@@ -97,11 +106,67 @@ But for testing, I've made the trace more human-readable.)
97106
Second, when you're "print debugging" in the backend, you can't simply print to stdout, since `pg_logger.py`
98107
*redirects* stdout to a buffer. Instead, you need to write all of your print statements as:
99108

100-
print >> sys.stderr, <debug message to print>
109+
```python
110+
print >> sys.stderr, <debug message to print>
111+
```
101112
102113
so that the output goes to stderr.
103114

104115
The easiest way to debug is to insert in print statements (to stderr) and then run `generate_json_trace.py` on
105116
small code examples.
106117

107-
###
118+
119+
### Backend control flow
120+
121+
Let's trace through a typical backend run to get a sense of how it works:
122+
123+
The main entry point is this function in `pg_logger.py`:
124+
125+
```python
126+
def exec_script_str(script_str, cumulative_mode, finalizer_func):
127+
```
128+
129+
`script_str` is the entire string contents of the Python program for the backend to execute.
130+
Ignore `cumulative_mode` for now (just set it to `False`). `finalizer_func` is the function to call
131+
after the backend is done generating a trace.
132+
133+
Let's look at how `generate_json_trace.py` calls `exec_script_str` (using a simplified version of the code):
134+
135+
```python
136+
import pg_logger, json
137+
138+
def json_finalizer(input_code, output_trace):
139+
ret = dict(code=input_code, trace=output_trace)
140+
json_output = json.dumps(ret, indent=2)
141+
print(json_output)
142+
143+
pg_logger.exec_script_str(open("example.py").read(), False, json_finalizer)
144+
```
145+
146+
In this simplified example, the script opens `example.py`, reads its contents into a string, and passes it
147+
into `exec_script_str`. The finalizer function is `json_finalizer`, which takes two parameters --
148+
the original code from `example.py` (`input_code`) and the execution trace that it produced (`output_trace`) --
149+
inserts both into a dict, encodes that dict as a JSON object, and then prints that JSON object to stdout.
150+
That's why when you run `generate_json_trace.py`, its output is a JSON object printed to stdout.
151+
152+
Note that if you pass in another finalizer function, then you can do other actions like postprocessing
153+
the output trace or saving it to a file rather than printing to stdout.
154+
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`:
157+
158+
```python
159+
def exec_script_str(script_str, cumulative_mode, finalizer_func):
160+
logger = PGLogger(cumulative_mode, finalizer_func)
161+
162+
try:
163+
logger._runscript(script_str)
164+
except bdb.BdbQuit:
165+
pass
166+
finally:
167+
logger.finalize()
168+
```
169+
170+
This code creates a `PGLogger` object then calls its `_runscript` method to run the script passed in as `script_str`.
171+
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`.

0 commit comments

Comments
 (0)