getting_started_with_sage_notebook/script.rst
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. Clearly state the objectives of the LO (along with RBT level)
       
     5 
       
     6 .. By the end of this tutorial, you should -- 
       
     7 
       
     8 ..   #. Know what Sage and Sage notebook are.
       
     9 ..   #. Be able to start a Sage shell or notebook
       
    10 ..   #. Be able to start using the notebook
       
    11 ..   #. Be able to create new worksheets 
       
    12 ..   #. Know about the menu options available 
       
    13 ..   #. Know about the cells in the worksheet
       
    14 ..   #. Be able to evaluate cells, create and delete cells, navigate them.
       
    15 ..   #. Be able to make annotations in the worksheet
       
    16 ..   #. Be able to use tab completion. 
       
    17 ..   #. Be able to use code from other languages in the cells.            
       
    18 
       
    19 .. Prerequisites
       
    20 .. -------------
       
    21 
       
    22 .. None. 
       
    23      
       
    24 .. Author              : Madhu
       
    25    Internal Reviewer   : Punch
       
    26    External Reviewer   :
       
    27    Language Reviewer   : Bhanukiran
       
    28    Checklist OK?       : <15-11-2010, Anand,  OK> [2010-10-05]
       
    29 
       
    30 
       
    31 Script
       
    32 ------
       
    33 
       
    34 {{{ Show the slide containing the title }}}
       
    35 
       
    36 Hello friends. Welcome to this spoken tutorial on Getting started with
       
    37 Sage and Sage notebook.
       
    38 
       
    39 {{{ Show the slide containing the outline }}}
       
    40 
       
    41 In this tutorial, we will learn what Sage is, what is Sage notebook,
       
    42 how to start and use the sage notebook. In the notebook we will be
       
    43 specifically learning how to execute our code, how to write
       
    44 annotations and other content, typesetting the content and how to use
       
    45 the offline help available.
       
    46 
       
    47 {{{ Show the slide on what is Sage }}}
       
    48 
       
    49 To start with, What is Sage? Sage is a free, open-source mathematical
       
    50 software. Sage can do a lot of math stuff for you including, but not
       
    51 limited to, algebra, calculus, geometry, cryptography, graph theory
       
    52 among other things. It can also be used as aid in teaching and
       
    53 research in any of the areas that Sage supports. So let us start Sage
       
    54 now
       
    55 
       
    56 {{{ Shift to terminal }}}
       
    57 
       
    58 We are assuming that you have Sage installed on your computer now. If
       
    59 not please visit the page
       
    60 http://sagemath.org/doc/tutorial/introduction.html#installation for
       
    61 the tutorial on how to install Sage. 
       
    62 
       
    63 
       
    64 Let us now learn how to start Sage. On the terminal type::
       
    65 
       
    66   sage
       
    67 
       
    68 This should start a new Sage shell with the prompt sage: which looks
       
    69 like this
       
    70 
       
    71 {{{ Show what is displayed on the terminal }}}
       
    72 
       
    73 So now we can type all the commands that Sage supports here. But Sage
       
    74 comes bundled with a much more elegant tool called Sage
       
    75 Notebook? What is Sage Notebook? Sage Notebook provides a web based
       
    76 user interface to use Sage. So once we have a Sage notebook server up
       
    77 and running, all we want is a browser to access the Sage
       
    78 functionality. For example there is an official instance of Sage
       
    79 Notebook server running at http://sagenb.org You can visit that page,
       
    80 create an account there and start using Sage! So all you need is just
       
    81 a browser, a modern browser 
       
    82 
       
    83 {{{ Intentional *cough* *cough* }}}
       
    84 
       
    85 #[[Bhanu: what? Are you serious?]]
       
    86 
       
    87 to use Sage and nothing else! The Sage notebook also provides a
       
    88 convenient way of sharing and publishing our work, which is very handy
       
    89 for research and teaching.
       
    90 
       
    91 However we can also run our own instances of Sage notebook servers on
       
    92 all the computers we have a local installation of Sage. To start the
       
    93 notebook server just type::
       
    94 
       
    95   notebook()
       
    96 
       
    97 on the Sage prompt. This will start the Sage Notebook server. If we
       
    98 are starting the notebook server for the first time, we are prompted
       
    99 to enter the password for the admin. Type the password and make a note
       
   100 of it. After this Sage automatically starts a browser page for you
       
   101 with the notebook opened.
       
   102 
       
   103 If it doesn't automatically start a browser page check if the Notebook
       
   104 server started and there were no problems. If so open your browser and
       
   105 in the address bar type the URL shown in the instructions upon running
       
   106 the notebook command on the sage prompt.
       
   107 
       
   108 {{{ The notebook() command gives an instruction telling 
       
   109 Open your web browser to http://localhost:8000. Point towards it }}}
       
   110 
       
   111 In our case it is http://localhost:{{{ Tell whatever is shown }}}
       
   112 
       
   113 {{{ Show the browser with Sage notebook }}}
       
   114 
       
   115 If you are not logged in yet, it shows the Notebook home page and
       
   116 textboxes to type the username and the password. You can use the
       
   117 username 'admin' and the password you gave while starting the notebook
       
   118 server for the first time. There are also links to recover forgotten
       
   119 password and to create new accounts.
       
   120 
       
   121 {{{ If you are logged in tell that you are logged in, log out and show
       
   122 what is said above for the login page }}}
       
   123 
       
   124 Once we are logged in with the admin account we can see the notebook
       
   125 admin page. A notebook can contain a collection of Sage Notebook
       
   126 worksheets. Worksheet is basically a working area. This is where
       
   127 we enter all the Sage commands on the notebook.
       
   128 
       
   129 The admin page lists all the worksheets created. On the topmost part
       
   130 of this page we have the links to various pages. 
       
   131 
       
   132 {{{ Whenever you talk on an individual link point your mouse towards
       
   133 the link. For each of the link go to the page and explain as below }}}
       
   134 
       
   135 The home link takes us to the admin home page. The published link
       
   136 takes us to the page which lists all the published worksheets. The log
       
   137 link has the complete log of all the actions we did on the
       
   138 notebook. We have the settings link where we can configure our notebook,
       
   139 the notebook server, create and  mangage accounts. We have a
       
   140 link to help upon clicking opens a new window with the complete help
       
   141 of Sage. The entire documentation of Sage is supplied with Sage for
       
   142 offline reference and this help link is the way to get into it. Then
       
   143 we can report bugs about Sage by clicking on Report a Problem link and
       
   144 there is a link to sign out of the notebook.
       
   145 
       
   146 We can create a new worksheet by clicking New Worksheet link
       
   147 
       
   148 {{{ Click on the link }}}
       
   149 
       
   150 Sage prompts you for a name for the worksheet. Let us name the
       
   151 worksheet as nbtutorial. Now we have our first worksheet which is
       
   152 empty.
       
   153 
       
   154 A worksheet will contain a collection of cells. Every Sage command
       
   155 must be entered in this cell. Cell is equivalent to the prompt on
       
   156 console. When we create a new worksheet, to start with we will have
       
   157 one empty cell. Let us try out some math here::
       
   158 
       
   159   2 + 2
       
   160   57.1 ^ 100
       
   161 
       
   162 The cap operator is used for exponentiation. If you observed carefully
       
   163 we typed two commands but the output of only last command was
       
   164 displayed. By default each cell displays the result of only the last
       
   165 operation. We have to use print statement to display all the results
       
   166 we want to be displayed.
       
   167 
       
   168 {{{ Demonstrate creating a new cell }}}
       
   169 
       
   170 Now to perform more operations we want more cells. So how do we create
       
   171 a new cell? It is very simple. As we hover our mouse above or below
       
   172 the existing cells we see a blue line, by clicking on this new line we
       
   173 can create a new cell. 
       
   174 
       
   175 We have a cell, we have typed some commands in it, but how do we
       
   176 evaluate that cell? Pressing Shift along with Enter evaluates the
       
   177 cell. Alternatively we can also click on the evaluate link to evaluate
       
   178 the cell
       
   179 
       
   180 {{{ Evaluate the cell and demonstrate for both methods separately
       
   181 }}}::
       
   182 
       
   183   matrix([[1,2], [3,4]])^(-1)
       
   184 
       
   185 After we create many cells, we may want to move between the cells. To
       
   186 move between the cells use Up and Down arrow keys. Also clicking on
       
   187 the cell will let you edit that particular cell.
       
   188 
       
   189 {{{ Move between two cells created }}}
       
   190 
       
   191 To delete a cell, clear the contents of the cell and hit backspace
       
   192 
       
   193 {{{ Clear and demonstrate deleting the cell }}}::
       
   194 
       
   195   Makes no sense
       
   196 
       
   197 If you want to add annotations in the worksheet itself on the blue
       
   198 line that appears on hovering the mouse around the cell, Hold Shift
       
   199 and click on the line. This creates a What You See Is What You Get
       
   200 cell.
       
   201 
       
   202 {{{ Create a HTML editable cell }}}
       
   203 
       
   204 We can make our text here rich text. We can make it bold, Italics, we
       
   205 can create bulleted and enumerated lists in this area::
       
   206 
       
   207   This text contains both the **bold** text and also *italicised*
       
   208   text.
       
   209   It also contains bulleted list:
       
   210   * Item 1
       
   211   * Item 2
       
   212   It also contains enumerate list:
       
   213   1. Item 1
       
   214   2. Item 2
       
   215 
       
   216 In the same cell we can display typeset math using the LaTeX like
       
   217 syntax::
       
   218 
       
   219   $\int_0^\infty e^{-x} \, dx$
       
   220 
       
   221 We enclose the math to be typeset within $ and $ or $$ and $$ as in
       
   222 LaTeX.
       
   223 
       
   224 We can also obtain help for a particular Sage command or function
       
   225 within the worksheet itself by using a question mark following the
       
   226 command::
       
   227 
       
   228   sin?
       
   229 
       
   230 Evaluating this cell gives me the entire help for the sin function
       
   231 inline on the worksheet itself. Similarly we can also look at the
       
   232 source code of each command or function using double question mark::
       
   233 
       
   234   matrix??
       
   235 
       
   236 Sage notebook also provides the feature for autocompletion. To
       
   237 autocomplete a command type first few unique characters and hit tab
       
   238 key::
       
   239 
       
   240   sudo<tab>
       
   241 
       
   242 To see all the commands starting with a specific name type those
       
   243 characters and hit tab::
       
   244 
       
   245   plo<tab>
       
   246 
       
   247 To list all the methods that are available for a certain variable or
       
   248 a datatype we can use the variable name followed by the dot to access
       
   249 the methods available on it and then hit tab::
       
   250 
       
   251   s = 'Hello'
       
   252   s.rep<tab>
       
   253 
       
   254 The output produced by each cell can be one of the three states. It
       
   255 can be either the full output, or truncated output or hidden output.
       
   256 The output area will display the error if the Sage code we wrote in
       
   257 the cell did not successfully execute::
       
   258 
       
   259   a, b = 10
       
   260 
       
   261 {{{ Show the three states }}}
       
   262 
       
   263 The default output we obtained now is a truncated output. Clicking at
       
   264 the left of the output area when the mouse pointer turns to hand gives
       
   265 us the full output, clicking again makes the output hidden and it
       
   266 cycles.
       
   267 
       
   268 Lastly, Sage supports a variety of languages and each cell on the
       
   269 worksheet can contain code written in a specific language. It is
       
   270 possible to instruct Sage to interpret the code in the language we
       
   271 have written. This can be done by putting percentage sign(%) followed
       
   272 by the name of the language. For example, to interpret the cell as
       
   273 Python code we put::
       
   274 
       
   275   %python
       
   276 
       
   277 as the first line in the cell. Similarly we have: %sh for shell
       
   278 scripting, %fortran for Fortran, %gap for GAP and so on. Let us see
       
   279 how this works. Say I have an integer. The type of the integer in
       
   280 default Sage mode is
       
   281 {{{ Read the output }}}::
       
   282 
       
   283   a = 1
       
   284   type(a)
       
   285 
       
   286   Output: <type 'sage.rings.integer.Integer'>
       
   287 
       
   288 We see that Integers are Sage Integers. Now let us put %python as the
       
   289 first line of the cell and execute the same code snippet::
       
   290 
       
   291   %python
       
   292   a = 1
       
   293   type(a)
       
   294 
       
   295   Output: <type 'int'>
       
   296 
       
   297 Now we see that the integer is a Python integer. Why? Because now we
       
   298 instructed Sage to interpret that cell as Python code.
       
   299 
       
   300 This brings us to the end of the tutorial on using Sage. We learnt
       
   301 quite a lot about using the Notebook User Interface of Sage. We are
       
   302 now confident that we can comfortably use the notebook to learn more
       
   303 about Sage in the following tutorials. Let us summarize what we
       
   304 learnt. In this session we learnt
       
   305 
       
   306   * What is Sage
       
   307   * How to start Sage shell
       
   308   * What is Sage notebook
       
   309   * How to start the Sage notebook
       
   310   * How to create accounts and start using the notebook
       
   311   * How to create new worksheets
       
   312   * The menus available on the notebook
       
   313   * About cells in the worksheet
       
   314   * Methods to evaluate the cell, create new cells, delete the cells
       
   315     and navigate around the cells
       
   316   * To make annotations in the worksheet
       
   317   * Tab completions
       
   318   * And embedding code of other scripting languages in the cells
       
   319 
       
   320 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   321 
       
   322 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   323 
       
   324 Hope you have enjoyed and found it useful.
       
   325 Thank you!