Getting started
===============

Installation
------------

Transcrypt is currently tested under Windows and Linux, OS X will probably follow later.
Installation for Windows and Linux is identical. For both OS'es follow the steps below:

1.	Install Python 3.5
2.	Download the Transcrypt zip and unpack it anywhere you like
3.	Add *../Transcrypt-<version>/Transcrypt to your system path* 
4.	Go to directory *../Transcrypt-<version>/Transcrypt/development/automated_tests/transcrypt*
5.	From the command prompt run *transcrypt -b autotest.py*. This will compile the autotests into file *autotest.js* and put it into the *__javascript__* subdirectory. Do NOT go to that directory (there's no need, stay where you went at point 4)
6.	From the command prompt run *transcrypt -r autotest.py*. This will run the autotests with CPython creating file *autotest.html* that refers to the generated *autotest.js* file
7.	Load the autotest.html into your browser, e.g. by clicking on it (tests were done with Chrome). It will load *autotest.js*, run it and compare the output with what was generated by CPython. It should report no errors

In case after point 3 you want to do something more than autotesting:

4.	Create a directory for your experiments
5.	Make your own thing there
6.	Compile as with step 5 of the previous, but now with your own source file instead of *autotest.py*
7.	Make an HTML page that will load your code in the browser. Use the HTML file generated by the autotest as an example of how to do that
8.	Load and run the HTML + JS

You may also want to try the demo's.

Troubleshooting checklist
~~~~~~~~~~~~~~~~~~~~~~~~~

1. Transcrypt was installed using *pip*, but *import transcrypt* fails. Transcrypt isn't a library but a compiler. Install and run it as described in this chapter.
2. Transcrypt reports an error containing the word 'java'. Transcrypt produces both prettyfied and minified JavaScript output. For the minification it makes use of the Google Closure Compiler, which is included in the distribution and requires Java to run. You can check proper installation of Java by typing the word *java* on the command line. This should give you a list of options: *Usage: java [-options] class []args...]* and so on. If you can't or won't install Java, you can run Transcrypt without minification by using the *-n* command line switch.
3. The static checker doesn't find all errors it could. The static checks, performed by the PyFlakes package that's part of the distribution, are of a 'light' variety. Style checks and false positives are avoided. The accent is on finding undefined identifiers and unused variables.

Your first Transcrypt program
-----------------------------

Open a command prompt in the *demos/hello* directory and type *transcrypt hello.py*.
Then click on *hello.html* to load it into your browser.

After clicking on both buttons a few times, take a look at *hello.html*.
As you can see, eventhandlers are connected to the buttons exactly as you would do with JavaScript.

Then look at *hello.py*.
Note that JavaScript functions like document.getElementById can be called from Python exactly as you would call them from JavaScript, but with Python data.

The minified JavaScript file *including the Transcrypt runtime* is only 10kB. With non-trivial programs this overhead becomes negligible. Transcrypt applications in themselves don't need any external files. Of course you can use extensive JavaScript libraries served by content delivery networks as you normally would. But you can also make very compact stand alone applications. The most efficient thing to do is pack all functionality for a page in a single Transcrypt program. Note that the source can consist of many modules if needed and many callback entrypoints can be provided. But it is *one* stateful application, just like it would be on the desktop.

.. literalinclude:: ../../demos/hello/hello.html
	:tab-width: 4
	:caption: In hello.html, Python handlers are attached directly to the onclick events
	
.. literalinclude:: ../../demos/hello/hello.py
	:tab-width: 4
	:caption: In hello.py, JavaScript function document.getElementById is called directly from Python, using plain Python values
	
.. _command_line_switches:
	
Command line switches
---------------------
The available command line switches will be shown if you run transcript -h.
They are specified in the source code of Transcrypt as follows:

.. literalinclude:: ../../modules/org/transcrypt/utils.py
	:pyobject: CommandArgs
	:tab-width: 4
	:caption: Transcrypt command line switches as specified in module/orgs/transcrypt/utils.py
	
If static checking is enabled, insert dummy definitions of global JavaScript variables between :ref:`__pragma__ ('skip') and __pragma__ ('noskip') <skipping_fragments>` to prevent needless complaints of the checker. The static checks are geared towards avoiding false alarms, and mainly check undefined names and unused variables. Style checks are deliberately avoided.
	