OLD | NEW |
(Empty) | |
| 1 {{+bindTo:partials.standard_nacl_article}} |
| 2 |
| 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 |
| 9 textual diffs. |
| 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> |
| 15 <dl class="docutils"> |
| 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> |
| 22 <script> |
| 23 var tag = document.getElementById('python_compat'); |
| 24 var tag2 = document.getElementById('python_compat2'); |
| 25 if (!('application/x-pnacl' in navigator.mimeTypes)) { |
| 26 tag.innerHTML = |
| 27 'This codelab does not appear to be supported by your browser.'; |
| 28 tag.style.color = '#cc0000'; |
| 29 tag2.innerHTML = |
| 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.'; |
| 33 } else { |
| 34 tag.innerHTML = 'This codelab appears to be supported by your browser.'; |
| 35 tag.style.color = '#00cc00'; |
| 36 } |
| 37 </script></li> |
| 38 <li>A fast broadband connection (500MB download)</li> |
| 39 <li>Can read and write Python</li> |
| 40 </ul> |
| 41 </dd> |
| 42 </dl> |
| 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-169-gc7
7aece/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 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:</p> |
| 57 <pre class="prettyprint"> |
| 58 WARNING: gcc not yet available for your platform (coming soon). |
| 59 bash.nmf-4.3$ |
| 60 </pre> |
| 61 <h3 id="our-web-based-tools">Our Web-based Tools</h3> |
| 62 <p>These development tools are a <a class="reference external" href="nacldev">wo
rk in progress</a>. |
| 63 At this point, they are a learning tool and demonstration of NaCl’s |
| 64 flexibility, but are not the recommended tools for a production application. |
| 65 In the future, that may change, but for the moment, |
| 66 to develop a substantial application for Native Client / |
| 67 Portable Native Client, |
| 68 we recommend you use the |
| 69 <a class="reference external" href="/native-client/sdk/download">Native Client S
DK</a>.</p> |
| 70 <b><font color="#880000"> |
| 71 NOTE: The NaCl Development Environment is not yet stable. |
| 72 Ideally user data is preserved, but currently it can be lost during updates |
| 73 or sporadically. We're working to resolve this. |
| 74 </font></b><h3 id="navigating-in-the-dev-environment">Navigating in the Dev Envi
ronment</h3> |
| 75 <p>The NaCl Dev Environment behaves like a mini-UNIX system. |
| 76 You start-up in bash command prompt. |
| 77 Standard UNIX commands like cd, ls, mkdir, rm, rmdir, etc. |
| 78 can be used to navigate and modify the Dev Environment’s |
| 79 virtual filesystem.</p> |
| 80 <p>There are 3 locations mounted by the environment:</p> |
| 81 <blockquote> |
| 82 <div><ul class="small-gap"> |
| 83 <li>/tmp mounts the HTML5 Filesystem temporary storage area.</li> |
| 84 <li>/mnt/html5 mounts the HTML5 Filesystem persistent storage area.</li> |
| 85 <li>Your home directory ~/, located in /home/user, |
| 86 mounts a portion of the HTML5 Filsystem persistent storage area |
| 87 that corresponds to the /mnt/html5/home directory.</li> |
| 88 </ul> |
| 89 </div></blockquote> |
| 90 <h3 id="clipboard">Clipboard</h3> |
| 91 <p>Many of the steps in this tutorial will be easier to copy and paste |
| 92 into the Dev Environment. |
| 93 To copy and paste in the Dev Environment web page, |
| 94 you can use the keyboard or the menu options under (☰ → Edit).</p> |
| 95 <p>On your platform use:</p> |
| 96 <blockquote> |
| 97 <div><ul class="small-gap"> |
| 98 <li><span id="copy_key">Loading...</span> |
| 99 <script> |
| 100 var tag = document.getElementById('copy_key'); |
| 101 if (navigator.appVersion.indexOf('Mac') >= 0) { |
| 102 tag.innerHTML = '⌘-C'; |
| 103 } else { |
| 104 tag.innerHTML = 'Ctrl-C'; |
| 105 } |
| 106 </script> to Copy</li> |
| 107 <li><span id="paste_key">Loading...</span> |
| 108 <script> |
| 109 var tag = document.getElementById('paste_key'); |
| 110 if (navigator.appVersion.indexOf('Mac') >= 0) { |
| 111 tag.innerHTML = '⌘-V'; |
| 112 } else { |
| 113 tag.innerHTML = 'Ctrl-V'; |
| 114 } |
| 115 </script> to Paste</li> |
| 116 </ul> |
| 117 </div></blockquote> |
| 118 <h4 id="editing">Editing</h4> |
| 119 <p>To follow along in this codelab, you’ll need to use a text editor to mo
dify |
| 120 various files in our development environment. |
| 121 There are currently two editor options, nano or vim. |
| 122 We have an Emacs port, but it is not yet available in the Dev Environment |
| 123 (coming soon). |
| 124 If you’re unsure what to pick, nano is simpler to start with and has on-sc
reen |
| 125 help.</p> |
| 126 <ul class="small-gap"> |
| 127 <li><p class="first">You can open <strong>nano</strong> like this:</p> |
| 128 <pre class="prettyprint"> |
| 129 $ nano <filename> |
| 130 </pre> |
| 131 <p>Here’s an online <a class="reference external" href="http://mintaka.sds
u.edu/reu/nano.html">nano tutorial</a>.</p> |
| 132 </li> |
| 133 <li><p class="first">You can open <strong>vim</strong> like this:</p> |
| 134 <pre class="prettyprint"> |
| 135 $ vim <filename> |
| 136 </pre> |
| 137 <p>Here’s an online <a class="reference external" href="http://www.openvim
.com/tutorial.html">vim tutorial</a>.</p> |
| 138 </li> |
| 139 </ul> |
| 140 <h3 id="git-setup">Git Setup</h3> |
| 141 <p>This tutorial also uses a revision control program called |
| 142 <a class="reference external" href="http://en.wikipedia.org/wiki/Git_(software)"
>git</a>. |
| 143 In order to commit to a git repository, |
| 144 you need to setup your environment to with your identity.</p> |
| 145 <p>Run these commands (with your info) to setup your <cite>~/.gitconfig</cite> |
| 146 for use:</p> |
| 147 <pre class="prettyprint"> |
| 148 git config --global user.name "John Doe" |
| 149 git config --global user.email johndoe@example.com |
| 150 </pre> |
| 151 <h2 id="get-the-code">Get the Code!</h2> |
| 152 <p>Rather than start from nothing, for this codelab we’ve provided |
| 153 you with a zip file containing a starting point.</p> |
| 154 <p>Download the codelab:</p> |
| 155 <pre class="prettyprint"> |
| 156 geturl https://nacltools.storage.googleapis.com/cds2014/cds2014_python.zip cds20
14_python.zip |
| 157 </pre> |
| 158 <p>Unzip it:</p> |
| 159 <pre class="prettyprint"> |
| 160 unzip cds2014_python.zip |
| 161 </pre> |
| 162 <p>Go into the codelab directory:</p> |
| 163 <pre class="prettyprint"> |
| 164 cd cds2014_python |
| 165 </pre> |
| 166 <p>Create a new local git repo:</p> |
| 167 <pre class="prettyprint"> |
| 168 git init |
| 169 </pre> |
| 170 <p>Add everything:</p> |
| 171 <pre class="prettyprint"> |
| 172 git add . |
| 173 </pre> |
| 174 <p>Commit it:</p> |
| 175 <pre class="prettyprint"> |
| 176 git commit -am "initial" |
| 177 </pre> |
| 178 <p>While working, you can see what you’ve changed by running:</p> |
| 179 <pre class="prettyprint"> |
| 180 git diff |
| 181 </pre> |
| 182 <h2 id="your-challenge-should-you-choose-to-accept-it">Your challenge, should yo
u choose to accept it...</h2> |
| 183 <p>Javascript has many wonderful features out of the box. |
| 184 Unfortunately, generating textual diffs is not one of them. |
| 185 Python on the other hand has the <a href="https://docs.python.org/2/library/diff
lib.html" |
| 186 target="_blank">difflib</a> module in its standard library.</p> |
| 187 <p>The starting point you’ve just extracted contains the shell |
| 188 of a web app using Portable Native Client Python to generate a diff. |
| 189 Just one thing is missing, that whole diffing thing...</p> |
| 190 <p>To see where things stand, deploy the sample like this:</p> |
| 191 <pre class="prettyprint"> |
| 192 make |
| 193 </pre> |
| 194 <p>This will copy the sample to /tmp/differ (clobbering whatever is already |
| 195 there). It will also attempt to open the sample, but will be blocked by |
| 196 a popup blocker. Click on the URL to accept the popup.</p> |
| 197 <p>As you can see, this isn’t quite what we’re going for.</p> |
| 198 <p>You’ll want to modify diff.py, using the editor you selected earlier. |
| 199 You’ll probably want to consult the <a href="https://docs.python.org/2/lib
rary/difflib.html" |
| 200 target="_blank">difflib</a> documentation. |
| 201 I would suggest you check out the HtmlDiff class and make use of |
| 202 the make_file member function. I would also suggest the splitlines |
| 203 function may come in handy.</p> |
| 204 <p>You can test diff.py manually as you would in a normal UNIX environment. |
| 205 It can be run like this:</p> |
| 206 <pre class="prettyprint"> |
| 207 ./diff.py before.txt after.txt out.html |
| 208 </pre> |
| 209 <p>Once everything is working diff.html will contain an html diff. |
| 210 Run <cite>make</cite> again to deploy and test the full app.</p> |
| 211 <p>Now get to it, and good luck!</p> |
| 212 <h2 id="what-you-ve-learned">What you’ve learned</h2> |
| 213 <p>While the details of building and packaging Python have been |
| 214 insulated from you for the purpose of this exercise, the key take-home lesson |
| 215 is how easy it is to leverage Python using PNaCl. |
| 216 As you’ve likely experienced, the initial start time is non-trivial. |
| 217 We’re working on improving this, both by improving PNaCl, |
| 218 and our Python port.</p> |
| 219 <p>The same approach to deploying Python apps can be used for the other |
| 220 interpreted languages that have been ported to PNaCl.</p> |
| 221 <p>Check out the range of interpreters, libraries, and tools |
| 222 <a class="reference external" href="https://code.google.com/p/naclports/wiki/Por
tList">already ported to PNaCl and ready to be integrated with your Web App</a>.
</p> |
| 223 <p>While our in-browser environment is rapidly evolving |
| 224 to become a complete development solution, |
| 225 for the broadest range of development options, check out the |
| 226 <a class="reference external" href="https://developer.chrome.com/native-client/s
dk/download">NaCl SDK</a>.</p> |
| 227 <p>For questions, concerns, and help contact us at |
| 228 <a class="reference external" href="https://groups.google.com/forum/#!forum/nati
ve-client-discuss">native-client-discuss@googlegroups.com</a>.</p> |
| 229 <p>Bring your interpreted app to PNaCl today!</p> |
| 230 <h2 id="cleanup">Cleanup</h2> |
| 231 <p>The Web-based version of the Chrome Dev Environment installs |
| 232 package data (>800MB) into the HTML5 Filesystem storage associated |
| 233 with naclports.storage.googleapis.com . |
| 234 To recover this storage (i.e. uninstall), |
| 235 enter <strong>chrome://settings/cookies</strong> in the Chrome URL bar. |
| 236 In the cookies UI, search for “naclports”. |
| 237 Then click the “x” next to the naclports entry.</p> |
| 238 </section> |
| 239 |
| 240 {{/partials.standard_nacl_article}} |
OLD | NEW |