Skip to content

Commit f9489f7

Browse files
committed
Merge branch 'master' of github.com:pgbovine/OnlinePythonTutor
2 parents fd97121 + 2dc3027 commit f9489f7

7 files changed

Lines changed: 94 additions & 64 deletions

File tree

‎README.md‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
Python Tutor -- http://pythontutor.com/ -- helps people overcome a fundamental barrier to learning programming: understanding what happens as the computer executes each line of a program's source code.
2-
3-
Using this tool, you can write Python, Java, JavaScript, TypeScript, Ruby, C, and C++ programs in your Web browser and visualize what the computer is doing step-by-step as it executes those programs. So far, over 1.5 million people in over 180 countries have used Python Tutor to visualize over 15 million pieces of code, often as a supplement to textbooks, lecture notes, and online programming tutorials.
1+
Python Tutor -- http://pythontutor.com/ -- helps people overcome a fundamental barrier to learning programming: understanding what happens as the computer executes each line of a program's source code. Using this tool, you can write Python, Java, JavaScript, TypeScript, Ruby, C, and C++ programs in your Web browser and visualize what the computer is doing step-by-step as it executes those programs.
42

53
This tool was created by [Philip Guo](http://pgbovine.net/) in January 2010. [See project history](history.txt).
64

7-
All documentation is viewable online at:
8-
https://github.com/pgbovine/OnlinePythonTutor/tree/master/v3/docs
5+
- [Frequently Asked Questions](v3/docs/user-FAQ.md)
6+
- [Overview for Developers](v3/docs/developer-overview.md)
97

8+
All documentation is viewable online at: https://github.com/pgbovine/OnlinePythonTutor/tree/master/v3/docs
109

11-
### Quick Start
1210

13-
By far the most preferred way to use Python Tutor is via the official website: http://pythontutor.com/
11+
### Quick Start
1412

15-
You can use [iframe embedding](v3/docs/embedding-HOWTO.md) to quickly embed visualizations on your webpage.
13+
BY FAR the most preferred way to use Python Tutor is via the official website, since it contains the latest updates: http://pythontutor.com/
1614

17-
It can be hard to run your own version locally for non-Python languages, since there are complex Docker-related setups in v4-cokapi/ that I haven't yet cleanly packaged up.
15+
You can use [iframe embedding](v3/docs/embedding-HOWTO.md) to easily embed visualizations on your webpage.
1816

19-
To run Python visualizations on localhost:
17+
If you want to run locally on your own computer, to run Python visualizations try:
2018

2119
```
2220
pip install bottle # make sure the bottle webserver (http://bottlepy.org/) is installed
@@ -28,20 +26,22 @@ You should see the visualizer at: http://localhost:8003/visualize.html
2826

2927
... and the live programming environment at: http://localhost:8003/live.html
3028

29+
However, it can be hard to run your own visualizer locally for non-Python languages, since there are complex setups in v4-cokapi/ that I haven't yet cleanly packaged up.
30+
3131
For further directions, see [Overview for Developers](v3/docs/developer-overview.md) or explore the [rest of the docs](v3/docs/).
3232

3333

3434
### Acknowledgments
3535

3636
For code or security contributions
3737

38-
- John DeNero - for helping with the official Python 3 port and LOTS of code patches
38+
- John DeNero - for helping with the official Python 3 port and lots of code patches
3939
- Chris Horne - https://github.com/lahwran - for security tips
4040
- Joshua Landau - joshua@landau.ws - for security tips
4141
- David Wyde - https://davidwyde.com/ - for security tips
42-
- Peter Wentworth and his students - for working on the original Python 3 fork circa 2011
42+
- Peter Wentworth and his students - for working on the original Python 3 fork circa 2010/2011
4343
- Brad Miller - for adding pop-up question dialogs to visualizations, and other bug fixes
44-
- David Pritchard and Will Gwozdz - for the Java frontend and other frontend enhancements
44+
- David Pritchard and Will Gwozdz - for the Java visualizer and other frontend enhancements
4545
- Peter Robinson - for v3/make_visualizations.py
4646
- Chris Meyers - for custom visualizations such as v3/matrix.py and v3/htmlFrame.py
4747
- Irene Chen - for holistic visualization mode -- v3/js/holistic.js

‎v3/docs/developer-overview.md‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ Look at the Git history to see when this document was last updated; the more tim
99
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,
12-
and general [command-line BS](http://www.pgbovine.net/command-line-bullshittery.htm),
13-
so I won't do much hand-holding in these directions.
12+
and [command-line BS](http://www.pgbovine.net/command-line-bullshittery.htm).
1413

15-
This guide isn't meant to be comprehensive; rather, it's just a starting point for learning about the code
16-
and development workflow. You will undoubtedly still be confused about details after reading it, so
14+
This guide isn't meant to be comprehensive; rather, it's a starting point for learning about the code
15+
and development workflow. You may still be confused about details after reading it, so
1716
feel free to email philip@pgbovine.net if you have questions.
1817

1918

20-
## Getting Started: Running OPT locally on your machine using Bottle:
19+
## Running Python Tutor locally on your machine using Bottle:
2120

2221
First install the [bottle](http://bottlepy.org/) micro web framework:
2322

@@ -28,26 +27,25 @@ And then run:
2827

2928
cd OnlinePythonTutor/v3/
3029
python bottle_server.py
30+
# python3 bottle_server.py # if you want to run for Python 3
3131

32-
(Use `python3` if you want to test the Python 3 backend. Note that when you run bottle, you will **always trigger the backend for the version of Python used to invoke it**, so the Python 2/3 toggle selector on the frontend is meaningless. You will also not be able to test other language backends locally.)
32+
Use `python3` if you want to test the Python 3 backend. Note that when you run with bottle, you will **always trigger the backend for the version of Python used to invoke it**, so the Python 2/3 language toggle selector on the frontend is meaningless. You will also not be able to test other language backends locally.
3333

3434

35-
If all goes well, when you visit this URL, you should see the Python Tutor visualizer:
35+
If all goes well, when you visit this URL, you should see the Python Tutor visualizer: http://localhost:8003/visualize.html
3636

37-
http://localhost:8003/visualize.html
38-
39-
Note that you can run bottle with both Python 2 and 3.
37+
And the live programming mode is here: http://localhost:8003/live.html
4038

4139
However, **only** run this app locally for testing, not in production, since all security checks are disabled.
4240

4341

4442
## Overall system architecture
4543

4644
OPT consists of a pure-Python backend and an HTML/CSS/JavaScript frontend.
47-
Here is a typical user interaction sequence:
45+
Here is a typical user interaction sequence for Python (this will differ for other languages):
4846

4947
1. The user visits [visualize.html](http://pythontutor.com/visualize.html) and types in Python code in the web-based text editor.
50-
2. The user hits the "Visualize execution" button.
48+
2. The user hits the "Visualize Execution" button.
5149
3. The OPT frontend sends the user's Python code as a string to the backend by making an Ajax GET request.
5250
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 sends that trace back to the frontend in JSON format.
5351
5. The frontend switches to a visualization display, parses the execution trace, and renders the appropriate stack frames, heap objects, and pointers.
@@ -60,6 +58,7 @@ The frontend consists of:
6058
visualize.html
6159
css/opt-frontend.css
6260
js/opt-frontend.js
61+
js/opt-frontend-common.js
6362
css/pytutor.css
6463
js/pytutor.js
6564
<a bunch of auxiliary css and js files such as libraries>
@@ -79,19 +78,19 @@ to embed on the same webpage without them interfering with one another.
7978
`opt-frontend.[js|css]` contain code that is specific to the `visualize.html` page and doesn't make sense for, say,
8079
embedding OPT visualizations into other webpages.
8180

82-
The backend consists of:
81+
The Python backend consists of:
8382
```
8483
pg_logger.py - the main entry point to the OPT backend
8584
pg_encoder.py - encodes the trace format into JSON to send to frontend
8685
generate_json_trace.py - script to test the backend independent of the frontend
87-
app.yaml, pythontutor.py - files for deploying on Google App Engine
86+
app.yaml, pythontutor.py - files for deploying on Google App Engine (obsolete)
8887
web_exec.py - example CGI script for deploying backend on CGI-enabled webservers
8988
```
9089

91-
The backend works with both Python 2 and 3. (Other language backends are located in [v4-cokapi/](https://github.com/pgbovine/OnlinePythonTutor/tree/master/v4-cokapi), not in v3/)
90+
This backend works with both Python 2 and 3. (Other language backends are located in [v4-cokapi/](https://github.com/pgbovine/OnlinePythonTutor/tree/master/v4-cokapi), not in v3/)
9291

9392

94-
## Hacking the backend
93+
## Hacking the Python backend
9594

9695
To modify the Python backend, you will mainly need to understand `pg_logger.py` and `pg_encoder.py`.
9796

@@ -115,7 +114,7 @@ This data is exactly what the backend sends to the frontend.
115114
But for testing purposes, I've made the trace more human-readable.)
116115

117116
Second, when you're "print debugging" in the backend, you can't simply print to stdout, since `pg_logger.py`
118-
*redirects* stdout to a buffer. Instead, you need to write all of your print statements as:
117+
*redirects* stdout to a buffer. Instead, you need to write all of your print statements (in Python 2) as:
119118

120119
```python
121120
print >> sys.stderr, <your debug message>
@@ -124,7 +123,7 @@ print >> sys.stderr, <your debug message>
124123
so that the output goes to stderr.
125124

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

130129

@@ -228,6 +227,7 @@ entry is created. At the end of execution, `self.trace` contains as many trace e
228227
in the user's program execution. Each step more-or-less corresponds to one line being executed.
229228
(To guard against infinite loops, `PGLogger` terminates execution when `MAX_EXECUTED_LINES` steps have been executed.)
230229

230+
231231
### Execution Trace Format
232232

233233
A lot of complicated stuff happens within `interaction` to grab a snapshot of the execution state and encode

‎v3/docs/embedding-HOWTO.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ Look at the Git history to see when this document was last updated; the more tim
99
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,
12-
and general [command-line BS](http://www.pgbovine.net/command-line-bullshittery.htm),
13-
so I won't do much hand-holding in these directions.
14-
15-
This guide isn't meant to be comprehensive; you will undoubtedly still
12+
and [command-line BS](http://www.pgbovine.net/command-line-bullshittery.htm). This guide isn't meant to be comprehensive; you may still
1613
be confused about details after reading it, so feel free to email
1714
philip@pgbovine.net if you have questions.
1815

1916

2017
## iframe embedding
2118

22-
An easy (although somewhat limited) way to embed an OPT visualization on your website is to enclose it within an [iframe](http://www.w3schools.com/tags/tag_iframe.asp).
19+
An easy way to embed an OPT visualization on your website is to enclose it within an [iframe](http://www.w3schools.com/tags/tag_iframe.asp).
2320

2421
If you generate a visualization (e.g., <a href="http://pythontutor.com/visualize.html#code=x+%3D+5%0Ay+%3D+10%0Az+%3D+x+%2B+y&mode=display&cumulative=false&py=2&curInstr=3">click here</a>)
2522
and then click the "Generate embed code" button at the bottom of the page, the following code will be generated:
@@ -32,7 +29,9 @@ and then click the "Generate embed code" button at the bottom of the page, the f
3229

3330
If you copy-and-paste the above code into your HTML webpage, then it will embed the given visualization as an iframe.
3431

35-
See `v3/iframe-embed-demo.html` for a working demo showing several embedded iframes ([online here](http://pythontutor.com/iframe-embed-demo.html)).
32+
Change `http://` to `https://` in the `src` attribute if you want to embed it in a webpage served over https.
33+
34+
See [v3/iframe-embed-demo.html](../iframe-embed-demo.html) for a working demo showing several embedded iframes ([online here](http://pythontutor.com/iframe-embed-demo.html)).
3635

3736

3837
### iframe embedding parameters
@@ -42,7 +41,7 @@ after the hash mark (`#` character) in the `src=` URL string. Note that OPT uses
4241
question mark `?` query string. Here are the currently-supported parameters:
4342

4443
- `code` - The Python code to visualize (mandatory: URL-encoded string)
45-
- `py` - Python interpreter version (mandatory: `2` for Python 2.7 or `3` for Python 3.2)
44+
- `py` - Language version (mandatory: `2` for Python 2.7 or `3` for Python 3.3, other languages: js, ts, ruby, java, c, cpp)
4645
- `verticalStack` - Set to `true` if you want the code and visualization to stack atop one another (optional)
4746
- `curInstr` - A (zero-indexed) integer of the execution point to directly jump to in the visualization (optional)
4847
- `cumulative` - Set to `true` if you want exited functions to be displayed (optional)
@@ -76,6 +75,7 @@ or, say, a USB drive), they can play with the visualization without an Internet
7675
Finally, multiple visualizations can be embedded in a single HTML webpage, although you need to be careful
7776
to redraw the SVG arrows when page elements are resized or moved.
7877

78+
7979
### The Nitty-Gritty
8080

8181
Let's attempt to go [literate programming](http://en.wikipedia.org/wiki/Literate_programming) style now ... load up

‎v3/docs/html-rendering.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# HTML rendering in Online Python Tutor
22

3+
[Warning: this doc hasn't been updated since 2013, so not sure how stale it is.]
4+
35
As of May 2013, Online Python Tutor contains an experimental magic
46
`setHTML` function that lets your scripts render HTML to the canvas (and
57
accompanying `setJS` and `setCSS` functions for JavaScript and CSS,

‎v3/docs/make_visualizations.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[This document was written by Peter Robinson (pjr@itee.uq.edu.au), the creator of make_visualizations.py]
1+
[This document was written in June 2013 by Peter Robinson (pjr@itee.uq.edu.au), the creator of make_visualizations.py]
22

33
# Using make_visualizations.py to build visualization embeddings
44

‎v3/docs/opt-trace-format.md‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ more time elapsed since that date, the more likely things are
1515
out-of-date.
1616

1717
I'm assuming that you're competent in Python, JavaScript, command-line-fu, and Google-fu,
18-
and general [command-line BS](http://www.pgbovine.net/command-line-bullshittery.htm),
19-
so I won't do much hand-holding in these directions.
18+
and [command-line BS](http://www.pgbovine.net/command-line-bullshittery.htm).
19+
You may still be confused about details after reading this doc, so
20+
feel free to email philip@pgbovine.net if you have questions.
2021

2122

2223
## Trace Overview
@@ -584,6 +585,23 @@ subsequent execution point. If we grep for `stdout` in the trace, we see the fol
584585
This isn't rocket science; but just be aware that `stdout` contains the cumulative contents of the stdout
585586
buffer up to that execution point, not only what's been printed by the most recently executed line.
586587

588+
### Potential stdout optimizations
589+
590+
The default trace format is completely unoptimized. It's designed so as to be "memoryless" -- i.e., a frontend can render an execution state by simply reading that entry and not any of its neighbors.
591+
592+
However, the `stdout` field can often take up A LOT OF SPACE since it's "cumulative" -- i.e., at each step, `stdout` represents the entire contents of the `stdout` buffer, not simply what's been printed out in the current step. Thus, it suffers from N^2 space explosions. Even a simple program like this one prints out an ENORMOUS amount to stdout:
593+
594+
```python
595+
choices = ['pizza', 'pasta', 'salad', 'nachos']
596+
for i in enumerate(choices):
597+
print choices # if you comment this out, the trace is much smaller
598+
choices.append(i)
599+
```
600+
601+
Thus, this is one target for optimization if deemed necessary down the line. However, I'm hesitant to optimize the trace since it makes trace-handling code in the frontend and backend more complicated :/
602+
603+
One elegant way to optimize is to simply store the completed `stdout` buffer ONCE in the trace. And then within each execution point, store a byte index into the buffer. Thus, to render the stdout contents for each point, all we do is slice the buffer from the start to that index. This is similar to the Unix-y idea of file pointers.
604+
587605

588606
## Function Stack Frames
589607

@@ -796,19 +814,3 @@ foo(1)
796814
[TODO: talk about appending `_z` onto `unique_hash` when the frame becomes a zombie.]
797815

798816
[TODO: IMPROVE ME!]
799-
800-
801-
# Notes: Potential optimizations
802-
803-
The default trace format is completely unoptimized. It's designed so as to be "memoryless" -- i.e., a frontend can render an execution state by simply reading that entry and not any of its neighbors.
804-
805-
However, the `stdout` field can often take up A LOT OF SPACE since it's "cumulative" -- i.e., at each step, `stdout` represents the entire contents of the `stdout` buffer, not simply what's been printed out in the current step. Thus, it suffers from N^2 space explosions. Even a simple program like this one prints out an ENORMOUS amount to stdout:
806-
807-
```python
808-
choices = ['pizza', 'pasta', 'salad', 'nachos']
809-
for i in enumerate(choices):
810-
print choices # if you comment this out, the trace is much smaller
811-
choices.append(i)
812-
```
813-
814-
Thus, this is one target for optimization if deemed necessary down the line. However, I'm hesitant to optimize the trace since it makes trace-handling code in the frontend and backend more complicated :/

0 commit comments

Comments
 (0)