1 {{+bindTo:partials.standard_nacl_article}}
3 <section id=
"what-a-difference-python-makes-codelab">
4 <span id=
"cds2014-python"></span><h1 id=
"what-a-difference-python-makes-codelab"><span id=
"cds2014-python"></span>What a Difference Python Makes - Codelab
</h1>
5 <h2 id=
"introduction">Introduction
</h2>
6 <p>Learn how you can use client-side Python on your webpage.
7 Use the python
<a href=
"https://docs.python.org/2/library/difflib.html"
8 target=
"_blank">difflib
</a> module to generate attractive online
10 Develop inside Google Chrome, using a hot off the presses version
11 of our NaCl Dev Environment, running on a webpage,
12 powered by Portable Native Client.
13 The techniques in this codelab also apply to other interpreted languages
14 we
’ve ported to PNaCl, including: Python, Lua, Ruby, Tcl, Bash, and Forth.
</p>
16 <dt>Requirements:
</dt>
17 <dd><ul class=
"first last small-gap">
18 <li>A Desktop / Laptop Windows, Mac, Linux, or ChromeOS browser
19 <br/><span id=
"python_compat" style=
"color: #cccc00"
20 >Checking browser compatibility...
</span><br/>
21 <i id=
"python_compat2"></i>
23 var tag
= document
.getElementById('python_compat');
24 var tag2
= document
.getElementById('python_compat2');
25 if (!('application/x-pnacl' in navigator
.mimeTypes
)) {
27 'This codelab does not appear to be supported by your browser.';
28 tag
.style
.color
= '#cc0000';
30 'You do not appear to be running a browser such as ' +
31 'Google Chrome which supports Portable Native Client ' +
32 'or you have disabled Portable Native Client.';
34 tag
.innerHTML
= 'This codelab appears to be supported by your browser.';
35 tag
.style
.color
= '#00cc00';
38 <li>A fast broadband connection (
500MB download)
</li>
39 <li>Can read and write Python
</li>
43 <h2 id=
"setup">Setup
</h2>
44 <p>For this codelab, you will need to point your web browser at
45 the Beta preview of our in-browser NaCl Dev Environment.
</p>
46 <a href=
"https://naclports.storage.googleapis.com/builds/pepper_41/trunk-176-g9b9e342/publish/devenv/pnacl/app/bash.html"
47 target=
"_blank">Click here to open the environment.
</a><p>While no installation is needed, the first load of the page will
48 require you to agree to allow the page unlimited storage
49 (Click
“Ok
” in the bar at the top of your browser window.)
</p>
50 <p>The first run requires a large download. Please be patient.
51 Also, as PNaCl translates executables to native code on demand,
52 each program will also have a noticable load delay on first run.
53 We
’re actively working to improve this.
</p>
54 <p>NOTE: This environment will leave a substantial (
800MB) payload in your
55 browser. See the cleanup section below to learn how to recover this space.
</p>
56 <p>Once the download is complete, you should see a bash prompt (ignore
57 the gcc warning, our web C compiler is coming soon!):
</p>
58 <pre class=
"prettyprint">
59 WARNING: gcc not yet available for your platform (coming soon).
62 <h3 id=
"our-web-based-tools">Our Web-based Tools
</h3>
63 <p>These development tools are a
<a class=
"reference external" href=
"nacldev">work in progress
</a>.
64 At this point, they are a learning tool and demonstration of NaCl
’s
65 flexibility, but are not the recommended tools for a production application.
66 In the future, that may change, but for the moment,
67 to develop a substantial application for Native Client /
68 Portable Native Client,
69 we recommend you use the
70 <a class=
"reference external" href=
"/native-client/sdk/download">Native Client SDK
</a>.
</p>
71 <b><font color=
"#880000">
72 NOTE: The NaCl Development Environment is not yet stable.
73 Ideally, user data is preserved, but currently it can be lost during updates
74 or sporadically. We're working to resolve this.
75 </font></b><h3 id=
"navigating-in-the-dev-environment">Navigating in the Dev Environment
</h3>
76 <p>The NaCl Dev Environment behaves like a mini-UNIX system.
77 You start-up in bash command prompt.
78 Standard UNIX commands like cd, ls, mkdir, rm, rmdir, etc.
79 can be used to navigate and modify the Dev Environment
’s
80 virtual filesystem.
</p>
81 <p>There are
3 locations mounted by the environment:
</p>
83 <div><ul class=
"small-gap">
84 <li>/tmp mounts the HTML5 Filesystem temporary storage area.
</li>
85 <li>/mnt/html5 mounts the HTML5 Filesystem persistent storage area.
</li>
86 <li>Your home directory ~/, located in /home/user,
87 mounts a portion of the HTML5 Filsystem persistent storage area
88 that corresponds to the /mnt/html5/home directory.
</li>
91 <h3 id=
"clipboard">Clipboard
</h3>
92 <p>Many of the steps in this tutorial will be easier to copy and paste
93 into the Dev Environment.
94 To copy and paste in the Dev Environment web page,
95 you can use the keyboard or the menu options (top right corner
☰ → Edit).
</p>
96 <p>On your platform use:
</p>
98 <div><ul class=
"small-gap">
99 <li><span id=
"copy_key">Loading...
</span>
101 var tag
= document
.getElementById('copy_key');
102 if (navigator
.appVersion
.indexOf('Mac') >= 0) {
103 tag
.innerHTML
= '⌘-C';
105 tag
.innerHTML
= 'Ctrl-C';
107 </script> to Copy
</li>
108 <li><span id=
"paste_key">Loading...
</span>
110 var tag
= document
.getElementById('paste_key');
111 if (navigator
.appVersion
.indexOf('Mac') >= 0) {
112 tag
.innerHTML
= '⌘-V';
114 tag
.innerHTML
= 'Ctrl-V';
116 </script> to Paste
</li>
119 <h4 id=
"editing">Editing
</h4>
120 <p>To follow along in this codelab, you
’ll need to use a text editor to modify
121 various files in our development environment.
122 There are currently two editor options, nano or vim.
123 We have an Emacs port, but it is not yet available in the Dev Environment
125 If you
’re unsure what to pick, nano is simpler to start with and has on-screen
127 <ul class=
"small-gap">
128 <li><p class=
"first">You can open
<strong>nano
</strong> like this:
</p>
129 <pre class=
"prettyprint">
130 $ nano
<filename
>
132 <p>Here
’s an online
<a class=
"reference external" href=
"http://mintaka.sdsu.edu/reu/nano.html">nano tutorial
</a>.
</p>
134 <li><p class=
"first">You can open
<strong>vim
</strong> like this:
</p>
135 <pre class=
"prettyprint">
136 $ vim
<filename
>
138 <p>Here
’s an online
<a class=
"reference external" href=
"http://www.openvim.com/tutorial.html">vim tutorial
</a>.
</p>
141 <h3 id=
"git-setup">Git Setup
</h3>
142 <p>This tutorial also uses a revision control program called
143 <a class=
"reference external" href=
"http://en.wikipedia.org/wiki/Git_(software)">git
</a>.
144 In order to commit to a git repository,
145 you need to set up your identity.
</p>
146 <p>Run these commands (with your info) to setup your
<cite>~/.gitconfig
</cite>
148 <pre class=
"prettyprint">
149 git config --global user.name
"John Doe
"
150 git config --global user.email johndoe
@example.com
152 <h2 id=
"get-the-code">Get the Code!
</h2>
153 <p>Rather than start from nothing, for this codelab we
’ve provided
154 you with a zip file containing a starting point.
</p>
155 <p>Download the codelab:
</p>
156 <pre class=
"prettyprint">
157 geturl https://nacltools.storage.googleapis.com/cds2014/cds2014_python.zip cds2014_python.zip
160 <pre class=
"prettyprint">
161 unzip cds2014_python.zip
163 <p>Go into the codelab directory:
</p>
164 <pre class=
"prettyprint">
167 <p>Create a new local git repo:
</p>
168 <pre class=
"prettyprint">
171 <p>Add everything:
</p>
172 <pre class=
"prettyprint">
176 <pre class=
"prettyprint">
177 git commit -am
"initial
"
179 <p>While working, you can see what you
’ve changed by running:
</p>
180 <pre class=
"prettyprint">
183 <h2 id=
"your-challenge-should-you-choose-to-accept-it">Your challenge, should you choose to accept it...
</h2>
184 <p>Javascript has many wonderful features out of the box.
185 Unfortunately, generating textual diffs is not one of them.
186 Python on the other hand has the
<a href=
"https://docs.python.org/2/library/difflib.html"
187 target=
"_blank">difflib
</a> module in its standard library.
</p>
188 <p>The starting point you
’ve just extracted contains the shell
189 of a web app using Portable Native Client Python to generate a diff.
190 Just one thing is missing, that whole diffing thing...
</p>
191 <p>To see where things stand, deploy the sample like this:
</p>
192 <pre class=
"prettyprint">
195 <p>This will attempt to open the sample, but will be blocked by
196 a popup blocker the first time. Click on the URL to accept the popup.
197 It also clobbers /tmp/differ for good measure.
</p>
198 <p>As you can see, this isn
’t quite what we
’re going for.
</p>
199 <p>You
’ll want to modify diff.py, using the editor you selected earlier.
200 You
’ll probably want to consult the
<a href=
"https://docs.python.org/2/library/difflib.html"
201 target=
"_blank">difflib
</a> documentation.
202 I would suggest you check out the HtmlDiff class and make use of
203 the make_file member function. Note our goal is to create a
204 full HTML diff, so the make_table function is insufficient.
205 The splitlines function may also come in handy.
</p>
206 <p>You can test diff.py manually as you would in a normal UNIX environment.
207 It can be run like this:
</p>
208 <pre class=
"prettyprint">
209 ./diff.py before.txt after.txt out.html
211 <p>Once everything is working, diff.html will contain an html diff.
212 After the initial
<cite>make
</cite> you can hit
“Run
” to test your changes.
</p>
213 <p>Now get to it, and good luck!
</p>
214 <h2 id=
"what-you-ve-learned">What you
’ve learned
</h2>
215 <p>While the details of building and packaging Python have been
216 insulated from you for the purpose of this exercise, the key take-home lesson
217 is how easy it is to leverage Python using PNaCl.
218 As you
’ve likely experienced, the initial start time is non-trivial.
219 We
’re working on improving this, both by improving PNaCl,
220 and our Python port.
</p>
221 <p>The same approach to deploying Python apps can be used for the other
222 interpreted languages that have been ported to PNaCl.
</p>
223 <p>Check out the range of interpreters, libraries, and tools
224 <a class=
"reference external" href=
"https://code.google.com/p/naclports/wiki/PortList">already ported to PNaCl and ready to be integrated with your Web App
</a>.
</p>
225 <p>While our in-browser environment is rapidly evolving
226 to become a complete development solution,
227 for the broadest range of development options, check out the
228 <a class=
"reference external" href=
"https://developer.chrome.com/native-client/sdk/download">NaCl SDK
</a>.
</p>
229 <p>Send us comments and feedback on the
<a class=
"reference external" href=
"https://groups.google.com/forum/#!forum/native-client-discuss">native-client-discuss
</a> mailing list,
230 or ask questions using Stack Overflow
’s
<a class=
"reference external" href=
"https://stackoverflow.com/questions/tagged/google-nativeclient">google-nativeclient
</a> tag.
</p>
231 <p>Bring your interpreted app to PNaCl today!
</p>
232 <h2 id=
"cleanup">Cleanup
</h2>
233 <p>The Web-based version of the Chrome Dev Environment installs
234 package data (
>800MB) into the HTML5 Filesystem storage associated
235 with naclports.storage.googleapis.com .
236 To recover this storage (i.e. uninstall),
237 enter
<strong>chrome://settings/cookies
</strong> in the Chrome URL bar.
238 In the cookies UI, search for
“naclports
”.
239 Then click the
“x
” next to the naclports entry.
</p>
242 {{/partials.standard_nacl_article}}