Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
407 lines (347 sloc) 19.5 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="static/img/favicon.ico">
<title>micro:bit Python Editor Help</title>
<!-- Bootstrap core CSS -->
<link href="static/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="static/css/starter-template.css" rel="stylesheet">
<link href="static/css/highlight.min.css" rel="stylesheet">
<link rel="stylesheet" href="static/css/font-awesome.css">
<!-- JS for contextual help -->
<script src="static/js/jquery-2.1.4.min.js"></script>
<script src="python-main.js"></script>
<script>
$(document).ready(function() {
window.location.search.substring(1).split("&").map(function(param) {
return param.split("=");
}).filter(function(pair) {
return pair[1] == "true";
}).forEach(function(pair) {
var enabledFlag = decodeURIComponent(pair[0]);
var hiddenFeatureClass = 'hide-' + enabledFlag;
$("." + hiddenFeatureClass).removeClass(hiddenFeatureClass);
});
});
</script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="https://python.microbit.org">Python on the micro:bit</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="#Editor">Editor</a></li>
<li><a href="#hello">Hello, World!</a></li>
<li class="hide-blocks"><a href="#blockly">Blockly</a></li>
<li class="hide-snippets"><a href="#snippets">Snippets</a></li>
<li><a href="#bugs">Bugs</a></li>
<li><a href="#more">Further Information</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="starter-template">
<div class="row" id="home">
<div class="col-md-8 col-md-offset-2">
<a href="http://microbit.org/"><img src="static/img/microbit.png"/></a>
<a href="http://micropython.org/"><img src="static/img/micropython.jpeg"/></a>
<a href="http://python.org/community/microbit/"><img src="static/img/python.png"/></a>
<br/></br/>
</div>
</div>
</div>
</div>
<div class="container">
<img src="static/img/comic1.png" class="img-responsive center-block"
alt="http://goo.gl/SrIrIE"/>
<h2 id="Editor"> The Editor</h2>
<p>The Python editor is perfect for those who want to push their coding skills further. A selection of snippets and a range of pre-made images and music give you a helping hand with your code. The editor is powered by the global <a href="http://python.org/community/microbit/">Python Community</a>.</p>
<h3 id="Menu">Menu</h3>
<p>The main menu of the editor contains various buttons that allow you to interact with the editor and the micro:bit device.
If you have additional features enabled in the editor, for example WebUSB you may see extra buttons availble in this menu.
</p>
<p><img src="static/img/header.png" class="image img-responsive center-block"
alt="A picture of the buttons along the top of the editor."/></p>
<p>As well as using a mouse, you can use the <kbd>TAB</kbd> key to navigate through the options and <kbd>Enter</kbd> to activate them. If you're in the text window, The <kbd>ESC</kbd> key will re-focus on the menu.
</p>
<table class="table">
<tr>
<td>
<div id="command-download" class="roundsymbol" title="Download a hex file to flash onto micro:bit" style="font-size: 0rem;">
<svg class="svg svg-icon-touchdevelop" viewBox="50 50 380 380" xmlns="http://www.w3.org/2000/svg" role="presentation" aria-label="touchdevelop,white">
<path d="m 185.96612,110.36806 41.029,41.029 0,-72.834699 27.352,0 0.001,72.834699 41.029,-41.029 0,31.911 -54.706,54.706 -54.705,-54.706 0,-31.911 z m 98.203,181.9 c 0,-9.128 7.406,-16.539 16.541,-16.539 9.146,0 16.535,7.411 16.535,16.539 0,9.131 -7.389,16.548 -16.535,16.548 -9.135,0 -16.541,-7.417 -16.541,-16.548 z m -121.494,0.003 c 0,-9.129 7.413,-16.542 16.548,-16.542 9.129,0 16.537,7.413 16.537,16.542 0,9.131 -7.408,16.547 -16.537,16.547 -9.135,0 -16.548,-7.416 -16.548,-16.547 z m 16.544,-81.93 122.554,0.202 c 45.667,0 82.285,36.553 82.285,81.732 0,45.179 -36.757,81.934 -81.937,81.934 l -122.902,0 c -45.177,0 -81.934395,-36.755 -81.934395,-81.934 0,-45.179 36.757395,-81.934 81.934395,-81.934 z m -49.164,81.934 c 0,27.105 22.059,49.164 49.164,49.164 l 122.902,0 c 27.107,0 49.159,-22.059 49.159,-49.164 0,-27.112 -22.052,-49.164 -49.159,-49.164 l -122.902,0 c -27.105,0 -49.164,22.052 -49.164,49.164 z" id="path3341" inkscape:connector-curvature="0" style="fill:currentColor"></path>
</svg>
</div>
</td>
<td>
Click on the "Download" button to save a special "hex" file on
computer. Plug in BBC micro:bit (it'll show up as USB
storage) and drag the newly saved file onto the device. Code
will run (or you'll see an error message scroll past on the
device's display). See below for how to extract code from a
hex file back into the editor.
</td>
</tr>
<tr>
<td>
<div id="command-save" class="roundsymbol" title="Save Python code to computer">
<i class="fa fa-download"></i>
</div>
</td>
<td>
Click on the "Save" button to save the code onto your computer.
Because you're downloading Python code computer might check
you want to save the file. It's trying to protect you from
downloading random software from the internet. But since this is
own code you're safe to proceed. See below for how to load
code back into the editor.
</td>
</tr>
<tr>
<td>
<div id="command-save" class="roundsymbol" title="Load Python code or .hex file into the editor">
<i class="fa fa-upload"></i>
</div>
</td>
<td>
It's very easy to load files from computer into the editor:
click the load button then drag the file from computer onto
the grey "drop" area. Alternatively, you could use the file
picker (just click on the link at the bottom to toggle between the
two options). The editor knows how to read Python files (whose name
ends with ".py") and extract Python code from hex files (whose name
ends with ".hex").
</td>
</tr>
<tr class="hide-blocks">
<td>
<div id="command-blockly" class="roundsymbol" title="Go to the blockly editor">
<i class="fa fa-puzzle-piece"></i>
</div>
</td>
<td>
<strong>This is an experimental feature. It may change in the future
and could contain bugs.</strong> Clicking the "Blockly" button
displays a set of drag-and-drop
blocks that can be used to quickly create Python code. Move the
blocks, watch Python code appear and change. <strong>Moving such
blocks will re-write Python code and you may lose work.
Furthermore, the arrangement of blocks is currently not saved,
just the resulting Python code.</strong>
</td>
</tr>
<tr class="hide-snippets">
<td>
<div id="command-snippet" class="roundsymbol" title="Load Python code or .hex file into the editor">
<i class="fa fa-cogs"></i>
</div>
</td>
<td>
Clicking on the "Snippets" button brings up a menu of Python
short-cuts (snippets). Code snippets are short blocks of code to
re-use in your own programs. There are snippets for most common
things you'll want to do with Python. Select the one you want and
fill in the gaps in the code editor to make it do what you want.
<strong class="hide-blocks">Snippets are disabled when blockly is enabled.</strong>
</td>
</tr>
<tr>
<td>
<div id="command-help" class="roundsymbol" title="Find help and support">
<i class="fa fa-question"></i>
</div>
</td>
<td>
Clicking on the "Help" button gets you here. But you knew that
already, right..? :-) Notice that editor is still available
but in a separate tab in browser.
</td>
</tr>
<tr class="hide-share">
<td>
<div id="command-help" class="roundsymbol" title="Share project">
<i class="fa fa-share-alt"></i>
</div>
</td>
<td>
Clicking on the "Share" button creates a unique link that points to
script. Add this link to bookmarks to retrieve code.
If you share this link others will be able to see code. So
code remains private you must set a password and an optional
password hint for it. <strong>This button is not available if
you're running the editor from a local file system.</strong>
</td>
</tr>
<tr>
<td align="center">
<div id="zoom-in" class="roundsymbol-sm" title="Zoom in">
<i class="fa fa-search-plus"></i>
</div>
<div id="zoom-out" class="roundsymbol-sm" title="Zoom out">
<i class="fa fa-search-minus"></i>
</div>
</td>
<td>
Everyone likes to show off their awesome Python skills. These
buttons are especially useful to zoom-in and zoom-out when you're
trying to show code to a large group of people via a
projector.
</td>
</tr>
</table>
<p>The name and description for your script is shown on the top
right hand side. Click them to edit them.</p>
<h3 id="text-window">Text window</h3>
<p>The other part of the editor is the text window, where we edit our scripts.</p>
<p><img src="static/img/editor_text.png"
class="image img-responsive center-block img-rounded"
alt="A picture of the text area in the code editor."/></p>
<p>The editor tries to help out by colouring the text to show what
all the different parts of program are. For example, Python keywords
(words built into the Python language) are grey. The brighter coloured
words are bits of the program <em>you</em> have created. Brown words
are constant values that never change and purple words represent
strings of characters to display. All the lines are numbered with the
current line highlighted.</p>
<h2 id="hello">Hello, World!</h2>
<p>A 'Hello, World!' program is traditionally used as a quick test to see if we've got
our software or hardware configured correctly.</p>
<p><img src="static/img/comic3.png" class="image img-responsive center-block"
alt="http://goo.gl/zpfrsY"/></p>
<p>Here's how we do this in the Python editor:</p>
<pre><code class="python">from microbit import *
display.scroll("Hello, World!")</code></pre>
<p>The first line (<code>from microbit import *</code>) tells <a href="#micropython">MicroPython</a>
to get all the bits of Python needed to program the micro:bit.</p>
<p>The second line (<code>display.scroll("Hello, World!")</code>) tells
MicroPython to use one of those bits of Python (the <code>display</code>
module) to scroll the text, <kbd>"Hello, World!"</kbd> across the physical
display on the front of the device.</p>
<p><img src="static/img/scroll-hello.gif" class="image img-responsive center-block img-rounded"
alt="An animation of 'hello world' scrolling over the display."/></p>
<p>When you load the Python editor in your browser, this is the default script you will see,
asking the micro:bit to scroll the words across the LED screen and then display an image of a heart.</p>
<p>Why not try making the micro:bit scroll your name?</p>
<div class="hide-blocks">
<h2 id="blockly">Blockly</h2>
<p>Use blockly to make new Python code from easy to assemble blocks of
code.</p>
<p><img src="static/img/blocks.png"
class="image img-responsive center-block img-rounded"
alt="An animation demonstrating how to use snippets."/></p>
<p>Blockly updates Python in the code editor as you move blocks around.
There are lots of different blocks to cover all the features of the
micro:bit. You can make more advanced code by combining blocks together
(such as in the example above).</p>
<p>You should use Blockly to create <em>new code</em> since moving blocks
around will overwrite any existing code you have in the editor (and you may
lose work as a result). The arrangement of blocks isn't (currently)
saved, so Blockly makes a great way to make simple Python code without
lots of typing. In the end, you'll learn that simply typing Python is by
far the quickest and easiest way to write code.</p>
<p>Not all the features of the micro:bit or MicroPython are available via
Blockly. We've done this on purpose. Some of the more advanced and
interesting stuff is only available in pure Python.</p>
<p>However, by using Blockly you'll very quickly get the hang of how Python
code works and fits together.</p>
</div>
<div class="hide-snippets">
<h2 id="snippets">Snippets</h2>
<p>Snippets are a cool way to avoid typing. The animation below shows you
how to use them:</p>
<p><img src="static/img/snippet_example.gif"
class="img-responsive center-block img-rounded"
alt="An animation demonstrating how to use snippets."/></p>
<p>You can access snippets from the "Snippets" button (see above), but
it's a lot quicker and easier to learn the <em>triggers</em> for the
different fragments of code, hit the <kbd>TAB</kbd> key to expand the
snippet and then fill in the remaining blocks of code so it does what you
want.</p>
<p class="hide-blocks">When blockly is enabled the "Snippets" button is disabled. This avoids
snippets from interfering with code that blockly may be generating.</p>
<p><img src="static/img/comic4.png" class="img-responsive center-block"
alt="http://goo.gl/bT8a8M"/></p>
</div>
<h2 id="bugs">Bug Fixes</h2>
<p>Things will go wrong!</p>
<p>You have to imagine Python is the most strict language teacher in the
universe... yes, even more strict than that really strict one you have at
school. Put simply, <strong>you have to type Python without any
mistakes</strong> for it to work.</p>
<p>All programmers make mistakes and create bugs. It's a fact of life.
When you have a bug MicroPython tries to help you out: it will scroll a
message on its display. It may even include a line number.</p>
<p>Common bugs include <em>Syntax Errors</em> (which means you've typed it in
wrong) and <em>Name Errors</em> (that mean you've typed in correct Python, but
it can't work out what you're coding about).</p>
<p>If you're unsure what to do
<a href="https://microbit-micropython.readthedocs.io/en/latest/">go read the
MicroPython docs</a> or <a href="https://support.microbit.org/en/support/tickets/new">open a support request with the micro:bit team</a>.
<h2 id="more">Further Information</h2>
<p><img src="static/img/comic5.png" class="img-responsive center-block"
alt="http://goo.gl/q5DAAW"/></p>
<h3 id="micropython">MicroPython</h3>
<p><img src="static/img/comic6.png" class="img-responsive center-block"
alt="http://goo.gl/9lcLAf"/></p>
<p>The version of Python that runs on the BBC micro:bit is called
MicroPython. It's exactly like <em>real</em> Python except it's designed
to run on small devices like the BBC micro:bit. You can find out more about it
on the <a href="http://micropython.org/">MicroPython site</a> and by reading the <a href="https://microbit-micropython.readthedocs.io/en/latest/">MicroPython for micro:bit reference</a>.</p>
<h3>How the editor works</h3>
<p>To see how the editor actually works? It's easy, to view
the "source code" press CTRL-U (or CMD-U on a Mac). There's a secret
message for you in the code. Can you find it?</p>
<p>The code for the editor lives in a <a href="https://github.com/bbcmicrobit/PythonEditor">repository on Github</a>. Github
makes it easy to raise a bug report and contribute to the project. For example, <a href="https://github.com/bbcmicrobit/PythonEditor/blob/master/help.html">here's the HTML code for this help page</a>.</p>
<h3>What next?</h3>
<p>Python makes it simple to write powerful programs. Can you work out what
this program does?</p>
<pre><code class="python"># Press button A for a fortune cookie.
from microbit import *
import random
fortunes = [
"Never step off a moving bus",
"This sentence is false",
"The meaning of life is overrated",
"Do not touch!",
"You will receive some advice",
"My hovercraft is full of eels",
]
while True:
if button_a.is_pressed():
cookie = random.choice(fortunes)
display.scroll(cookie)</code></pre>
<p>How could you improve it? What changes could you make?</p>
<footer style="text-align: center; margin-top: 64px;">
<p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons Licence" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/80x15.png" /></a> This work by <a xmlns:cc="http://creativecommons.org/ns#" href="http://python.org/community/microbit/" property="cc:attributionName" rel="cc:attributionURL">The Python Software Foundation</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
</p>
</footer>
</div><!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="static/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="static/js/ie10-viewport-bug-workaround.js"></script>
<script src="static/js/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>
You can’t perform that action at this time.