You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f9489f7
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: README.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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.
4
2
5
3
This tool was created by [Philip Guo](http://pgbovine.net/) in January 2010. [See project history](history.txt).
-[Overview for Developers](v3/docs/developer-overview.md)
9
7
8
+
All documentation is viewable online at: https://github.com/pgbovine/OnlinePythonTutor/tree/master/v3/docs
10
9
11
-
### Quick Start
12
10
13
-
By far the most preferred way to use Python Tutor is via the official website: http://pythontutor.com/
11
+
### Quick Start
14
12
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/
16
14
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.
18
16
19
-
To run Python visualizations on localhost:
17
+
If you want to run locally on your own computer, to run Python visualizations try:
20
18
21
19
```
22
20
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
28
26
29
27
... and the live programming environment at: http://localhost:8003/live.html
30
28
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
+
31
31
For further directions, see [Overview for Developers](v3/docs/developer-overview.md) or explore the [rest of the docs](v3/docs/).
32
32
33
33
34
34
### Acknowledgments
35
35
36
36
For code or security contributions
37
37
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
39
39
- Chris Horne - https://github.com/lahwran - for security tips
40
40
- Joshua Landau - joshua@landau.ws - for security tips
41
41
- 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
43
43
- 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
45
45
- Peter Robinson - for v3/make_visualizations.py
46
46
- Chris Meyers - for custom visualizations such as v3/matrix.py and v3/htmlFrame.py
47
47
- Irene Chen - for holistic visualization mode -- v3/js/holistic.js
Copy file name to clipboardExpand all lines: v3/docs/developer-overview.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,14 @@ Look at the Git history to see when this document was last updated; the more tim
9
9
elapsed since that date, the more likely things are out-of-date.
10
10
11
11
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).
14
13
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
17
16
feel free to email philip@pgbovine.net if you have questions.
18
17
19
18
20
-
## Getting Started: Running OPT locally on your machine using Bottle:
19
+
## Running Python Tutor locally on your machine using Bottle:
21
20
22
21
First install the [bottle](http://bottlepy.org/) micro web framework:
23
22
@@ -28,26 +27,25 @@ And then run:
28
27
29
28
cd OnlinePythonTutor/v3/
30
29
python bottle_server.py
30
+
# python3 bottle_server.py # if you want to run for Python 3
31
31
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.
33
33
34
34
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
36
36
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
40
38
41
39
However, **only** run this app locally for testing, not in production, since all security checks are disabled.
42
40
43
41
44
42
## Overall system architecture
45
43
46
44
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):
48
46
49
47
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.
51
49
3. The OPT frontend sends the user's Python code as a string to the backend by making an Ajax GET request.
52
50
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.
53
51
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:
60
58
visualize.html
61
59
css/opt-frontend.css
62
60
js/opt-frontend.js
61
+
js/opt-frontend-common.js
63
62
css/pytutor.css
64
63
js/pytutor.js
65
64
<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.
79
78
`opt-frontend.[js|css]` contain code that is specific to the `visualize.html` page and doesn't make sense for, say,
80
79
embedding OPT visualizations into other webpages.
81
80
82
-
The backend consists of:
81
+
The Python backend consists of:
83
82
```
84
83
pg_logger.py - the main entry point to the OPT backend
85
84
pg_encoder.py - encodes the trace format into JSON to send to frontend
86
85
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)
88
87
web_exec.py - example CGI script for deploying backend on CGI-enabled webservers
89
88
```
90
89
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/)
92
91
93
92
94
-
## Hacking the backend
93
+
## Hacking the Python backend
95
94
96
95
To modify the Python backend, you will mainly need to understand `pg_logger.py` and `pg_encoder.py`.
97
96
@@ -115,7 +114,7 @@ This data is exactly what the backend sends to the frontend.
115
114
But for testing purposes, I've made the trace more human-readable.)
116
115
117
116
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:
Copy file name to clipboardExpand all lines: v3/docs/embedding-HOWTO.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,17 +9,14 @@ Look at the Git history to see when this document was last updated; the more tim
9
9
elapsed since that date, the more likely things are out-of-date.
10
10
11
11
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
16
13
be confused about details after reading it, so feel free to email
17
14
philip@pgbovine.net if you have questions.
18
15
19
16
20
17
## iframe embedding
21
18
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).
23
20
24
21
If you generate a visualization (e.g., <ahref="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>)
25
22
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
32
29
33
30
If you copy-and-paste the above code into your HTML webpage, then it will embed the given visualization as an iframe.
34
31
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)).
36
35
37
36
38
37
### iframe embedding parameters
@@ -42,7 +41,7 @@ after the hash mark (`#` character) in the `src=` URL string. Note that OPT uses
42
41
question mark `?` query string. Here are the currently-supported parameters:
43
42
44
43
-`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)
46
45
-`verticalStack` - Set to `true` if you want the code and visualization to stack atop one another (optional)
47
46
-`curInstr` - A (zero-indexed) integer of the execution point to directly jump to in the visualization (optional)
48
47
-`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
76
75
Finally, multiple visualizations can be embedded in a single HTML webpage, although you need to be careful
77
76
to redraw the SVG arrows when page elements are resized or moved.
78
77
78
+
79
79
### The Nitty-Gritty
80
80
81
81
Let's attempt to go [literate programming](http://en.wikipedia.org/wiki/Literate_programming) style now ... load up
Copy file name to clipboardExpand all lines: v3/docs/opt-trace-format.md
+20-18Lines changed: 20 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,8 +15,9 @@ more time elapsed since that date, the more likely things are
15
15
out-of-date.
16
16
17
17
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.
20
21
21
22
22
23
## Trace Overview
@@ -584,6 +585,23 @@ subsequent execution point. If we grep for `stdout` in the trace, we see the fol
584
585
This isn't rocket science; but just be aware that `stdout` contains the cumulative contents of the stdout
585
586
buffer up to that execution point, not only what's been printed by the most recently executed line.
586
587
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 inenumerate(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
+
587
605
588
606
## Function Stack Frames
589
607
@@ -796,19 +814,3 @@ foo(1)
796
814
[TODO: talk about appending `_z` onto `unique_hash` when the frame becomes a zombie.]
797
815
798
816
[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 inenumerate(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