Debian Codegraph @

docs_sphinx/introduction/scripts.rst

raw
1 Running Brian scripts
2 =====================
3
4 Brian scripts are standard Python scripts, and can therefore be run in the same way.
5 For interactive, explorative work, you might want to run code in a
6 jupyter notebook or in an ipython shell; for running finished code, you might want
7 to execute scripts through the standard Python interpreter; finally, for working on
8 big projects spanning multiple files, a dedicated integrated development environment
9 for Python could be a good choice. We will briefly describe all these approaches and
10 how they relate to Brian's examples and tutorial that are part of this documentation.
11 Note that none of these approaches are specific to Brian, so you can also search for
12 more information in any of the resources listed on the
13 `Python website <https://www.python.org/about/gettingstarted/>`_.
14
15 .. contents::
16 :local:
17 :depth: 1
18
19 Jupyter notebook
20 ----------------
21 The Jupyter Notebook is an open-source web application that allows you to create
22 and share documents that contain live code, equations, visualizations and narrative text.
23
24 (from `jupyter.org <https://jupyter.org>`_)
25
26 Jupyter notebooks are a great tool to run Brian code interactively, and include
27 the results of the simulations, as well as additional explanatory text in a common
28 document. Such documents have the file ending ``.ipynb``, and in Brian we use this
29 format to store the :doc:`../resources/tutorials/index`. These files can be displayed by
30 github (see e.g. `the first Brian tutorial <https://github.com/brian-team/brian2/blob/master/tutorials/1-intro-to-brian-neurons.ipynb>`_),
31 but in this case you can only see them as a static website, not edit or execute any
32 of the code.
33
34 To make the full use of such notebooks, you have to run them using the
35 jupyter infrastructure. The easiest option is to use the free
36 `mybinder.org <https://mybinder.org>`_ web service, which allows you to try out
37 Brian without installing it on your own machine. Links to run the tutorials on this
38 infrastructure are provided as *"launch binder"* buttons on the
39 :doc:`../resources/tutorials/index` page, and also for each of the
40 :doc:`../examples/index` at the top of the respective page (e.g.
41 :doc:`../examples/COBAHH`). To run notebooks on your own machine, you need
42 an installation of the jupyter notebook software on your own machine, as well as
43 Brian itself (see the :doc:`install` instructions for details). To open an existing
44 notebook, you have to download it to your machine. For the Brian tutorials, you find
45 the necessary links on the :doc:`../resources/tutorials/index` page. When you have
46 downloaded/installed everything necessary, you can start the jupyter notebook from
47 the command line (using Terminal on OS X/Linux, Command Prompt on Windows)::
48
49 jupyter notebook
50
51 this will open the "Notebook Dashboard" in your default browser, from which you can
52 either open an existing notebook or create a new one. In the notebook, you can then
53 execute individual "code cells" by pressing ``SHIFT+ENTER`` on your keyboard, or
54 by pressing the play button in the toolbar.
55
56 For more information, see the
57 `jupyter notebook documentation <https://jupyter-notebook.readthedocs.io>`_.
58
59
60 IPython shell
61 -------------
62 An alternative to using the jupyter notebook is to use the interactive Python shell
63 `IPython <https://ipython.readthedocs.io/>`_, which runs in the
64 Terminal/Command Prompt. You can use it to directly type Python code interactively
65 (each line will be executed as soon as you press ``ENTER``), or to run Python code
66 stored in a file. Such files typically have the file ending ``.py``. You can
67 either create it yourself in a text editor of your choice (e.g. by copying&pasting
68 code from one of the :doc:`../examples/index`), or by downloading such files from
69 places such as github (e.g. the `Brian examples <https://github.com/brian-team/brian2/tree/master/examples>`_),
70 or `ModelDB <https://senselab.med.yale.edu/modeldb/>`_. You can then run them from
71 within IPython via::
72
73 %run filename.py
74
75
76 Python interpreter
77 ------------------
78 The most basic way to run Python code is to run it through the standard Python
79 interpreter. While you can also use this interpreter interactively, it is much less
80 convenient to use than the IPython shell or the jupyter notebook described above.
81 However, if all you want to do is to run an existing Python script (e.g. one of
82 the Brian :doc:`../examples/index`), then you can do this by calling::
83
84 python filename.py
85
86 in a Terminal/Command Prompt.
87
88 Integrated development environment (IDE)
89 ----------------------------------------
90 Python is a widely used programming language, and is therefore support by a wide
91 range of integrated development environments (IDE). Such IDEs provide features
92 that are very convenient for developing complex projects, e.g. they integrate
93 text editor and interactive Python console, graphical debugging tools, etc.
94 Popular environments include `Spyder <https://www.spyder-ide.org/>`_,
95 `PyCharm <https://www.jetbrains.com/pycharm/>`_, and
96 `Visual Studio Code <https://code.visualstudio.com/>`_, for an extensive list
97 see the `Python wiki <https://wiki.python.org/moin/IntegratedDevelopmentEnvironments>`_.