Skip to content

Examples and tests

Jean-Michel Laprise edited this page Nov 30, 2020 · 41 revisions

Examples of all the types of routes explained in the adding routes section can be found in the tests folder in the github repository. The main test file is autotest.py. Most of the functions it imports and tests can be found in simple_functions.py. You can donwload this folder and execute the tests yourself to get a better understanding of how autofront works. It's also included in the distribution if you know where to find it on your system (typically in your virtual environment in lib/pythonx.x/site-packages/autofront/tests).

While autofront attempts to accomodate a wide range of needs, a typical user wanting to use it as a remote control and display for a project might only ever need the simplest route:

autofront.add(my_function)

There are five differents test scripts used for autofront.

Running the test suite can be a great introduction to autofront's possibilities. It also lets you know if autofront is working properly on your system. Since it's still in early alpha, this is not guaranteed. You can also have a look at the custom static and template tests to discover the syntax. The other two scripts are mostly used for code-testing during development. Anyone wanting to contribute to autofront's development should run all five test scripts to ensure their proposed changes don't break anything.

Test suite

autotest.py is the main test suite for autofront. It's also a good reference for the syntax used in different routes. If you're having trouble adding a route, the first step should probably be to find a similar route in autotest and make sure it works on your system. If it does, check to make sure you've followed the same syntax when adding your route.

If you're having problems running routes in general, the first step should be to make sure autotest.py runs correctly on your system. Here is the code, required input (if any) and the expected output for each route in the test suite.

NOTE: Most routes specify join=True for faster execution. This is only possible for routes that do not use input calls, as explained here. This is optional and beginners can simply ignore the join keyword argument.

Basic route to function

autofront.add(foo, join=True)

Expected output

bar

This is the basic syntax used to execute a function that requires no arguments at all. It uses a print call and returns nothing.

Basic route to function with fixed arguments

autofront.add(foo_args, 'arg1', 'arg2', kwarg1='Surprise!', kwarg2='Aha!', join=True)

Expected output:

arg1 arg2 Surprise! Aha!

Use this syntax to run a function using the same arguments each time. Start with the function itself, then the positional arguments in order, then the keyword arguments in any order.

Return value test

autofront.add(return_value, join=True)

Expected output:

Return value: foo

This also follows the basic syntax, but the output comes from the return value instead of a print call.

Basic route to script

autofront.add('simple_script.py', join=True)

Expected output:

__main__
[local script filepath]
It works!

This is the basic syntax used to run a script with no command line arguments. It should output its __name__ attribute (which should be __main__), its __file__ attribute (which should point to the autofront local directory), and finally a success message ('It works!').

Basic route to script with fixed arguments

autofront.add('simple_script_args.py', 'foo', 'bar', 'foobar', join=True)

Expected output:

It works!
Here are your args:
'foo'
'bar'
'foobar'

Use this syntax to run a script with the same fixed command-line arguments every time. Start with the script path, then the command-line arguments in order. If your script is not in the same directory, you'll need to specify the full absolute or relative path.

Title syntax for duplicate functions and scripts

autofront.add('simple_script_args.py', link='forgotargs', join=True,
		       title='simple_script_args.py without args')

Expected output:

It works!
But you forgot the args

This points to the same script as the previous route. Routes pointing to the same script or function must be differentiated using the 'title' keyword argument. This will be the text used to identify the function in the browser. The 'link' keyword argument is optional and lets you specify a specific url to point to. By default, this is identical to 'title' and there is typically no reason to change it.

Route to test proper import behavior

autofront.add('import_script.py', join=True)

Expected output:

[System path]
[Current working directory]
[__file__]

The system path should start with the autotest.py directory, followed by the directory where autofront is installed. The current working directory should be where autotest.py is located. __file__ should be a script located in autofront/local in your autofront installation.

Route to script with user input

autofront.add('input_script.py')

Required input:

Script should ask you twice to input something in the browser. Choose what you want.

Expected output:

first input: [your first input]
second input: [your second input] 

This tests that script input calls function properly. Clicking on this route should open a browser page asking for input two times and print the result in the browser. There is no need to change the route syntax unless input detection fails. If this happens, you can add the keyword argument 'input_call=True' to force autofront to expect a possible input call.

Route to script which doesn't always ask for input

autofront.add('conditional_input_script.py', live=True)

Required input:

If you enter a single argument in the live argument box on the main page, the script will ask you to play a simple matching game by entering it a second time on the input page.

If you enter more than one argument on the main page, it won't ask for input.

Expected output:

If you entered an argument in the live argument box and the same one on the input page:

You won!

If you entered an argument in the live argument box and a different one on the input page:

You lost!

If you entered more than one argument in the live args:

Can't play match the args with so many args!

Route to cat game

autofront.add('test_joelle.py')

This is a simple cat game with a hidden ending, written by my niece. I keep it in the test suite out of nostalgia, as it is conceptually similar to the regular input script test. It did catch one bug that had slipped through! Feel free to play it or not as you wish.

Route to function with user input

autofront.add(input_function)

Required input:

Enter any two values in the input prompts.

Expected output:

Return value: They said [input1] and [input2]

This test checks if input functions work properly. It's basically identical to the input script test, except that it's a function.

Route to script meant to run in background

autofront.add('background_script.py', join=False)

The join=False keyword argument tells autofront not to wait for the script to finish execution before returning to the main page. The same syntax works for functions.

Required action:

Before running this test, make sure to delete logger.txt in the tests folder.

Expected output:

After 3 seconds, the script should write Time elapsed to a file called logger.txt in the tests folder.

Route to script that times out before completion

autofront.add('background_script.py', join=True, title='timeout test', timeout=2)

Expected output:

background_script.py timed out before completion.
You can change the timeout value with a kwarg:
autofront.add(my_function, timeout=value_in_seconds)

This uses the same script, but uses a timeout value lower than the script takes to complete (timeout=2) and waits for it to complete (join=True). It would still wait for completion without the join kwarg, as long as it's not set to False.

Route requiring live positional arguments

autofront.add(positional, live=True, join=True)

Required input:

Type the following in the input box: foo, bar

Expected output:

Positional bar foo

Tests live positional arguments. All arguments are interpreted as strings unless the typed=True keyword argument is used.

Route requiring live keyword arguments

autofront.add(keywords, live=True, join=True)

Required input:

kwarg1=foo, kwarg2=bar

Expected output:

Keywords: bar foo

Tests live keyword arguments. Ignores spaces around the equal sign if any.

Route with live positional and keyword arguments

autofront.add(combined, live=True, join=True)

Required input:

foo, bar, kwarg1=foobar, kwarg2=barfoo

Expected output:

Combined: barfoo foobar bar foo

Route with a mix of fixed and live arguments

autofront.add(mixed_args, 'fixed1', 'fixed2', live=True, join=True)

Required input:

foo, bar, kwarg1=foobar, kwarg2=barfoo

Expected output:

Builtin: foobar barfoo bar foo fixed2 fixed1

This syntax is for functions that combine built-in positional arguments with live arguments.

Route with live arguments and a return value

autofront.add(return_value_args, live=True, join=True)

Required input:

foo, bar, kwarg1=foobar, kwarg2=barfoo

Expected output:

Return value: ['foo', ' bar', 'foobar', 'barfoo']

Route to script with live command-line arguments

autofront.add('simple_script_live.py', live=True, join=True)

Required input:

Any number of words separated by spaces. Ex: arg1 arg2

Expected output:

Runtime arguments: [local script filepath, 'arg1', ' arg2']

Note that the syntax for scripts uses the standard command-line syntax with arguments separated by spaces instead of commas. To keep things simple for the user, separating arguments with commas will also work.

Route using live positional arguments with type indications

autofront.add(types, live=True, typed=True, join=True)

Required input:

bool:True, list:[int:3, str:4], dict:{str:foo : tuple:(float:3.4, int:4)} 

Expected output:

Here are your args and their type:
True <class 'bool'>
[3, '4'] <class 'list'>
{'foo': (3.4, 4)} <class 'dict'>

Go here to read more about the type indication syntax.

Route using a mix of live keyword and positional arguments with type indications

autofront.add(types_kwarg, live=True, typed=True, join=True)

Required input:

str:foo, int:4, str:bool1=bool:True, str:bool2=bool:False

Expected output:

Here are your args and their type:
foo <class 'str'>
4 <class 'int'>
Here are your kwargs and their type:
Key: bool1 <class 'str'>
Value: True <class 'bool'>
Key: bool2 <class 'str'>
Value: False <class 'bool'>

Route with typed arguments and a return value

autofront.add(return_value_types_args, live=True, typed=True, join=True)

Required input:

int:3, str:a, str:kwarg1=int:4

Expected output:

Return value: [(3, 'a'), {'kwarg1': 4}]

Route to a function that should print an exception

autofront.add(bugged_function)

Expected output:

IndexError
list index out of range

By default, autofront displays exceptions encoutered by your functions and scripts in the browser. This behavior can be changed to print them in the console when initializing autofront:

autofront.initialize(print_exceptions=False)

Note that exceptions raised by autofront itself will be raised as normal and the error message will print to the console, not the browser. It's also possible that some exceptions in your functions and scripts will fail to print to the browser. When an exception can't be handled by autofront, an error page will be displayed and you can find the debugging information in the console.

Route to a script that should print an exception

autofront.add('bugged_script.py')

Expected output:

Traceback (most recent call last):
File "/Users/jimmy/Programming/Python/autofront/autofront/local/bugged_script.py", line 9, in <module>
divsion_by_zero = 1/0
ZeroDivisionError: division by zero

Script execution exceptions should also be displayed in the browser.

Custom CSS and Template tests

Custom CSS test

statictest.py is used to make sure custom static folders are loaded properly.

Expected output:

This should simply load the standard test suite. Text should be in red.

NOTE: Because browsers cache CSS files, the text might appear not to have changed. Either clear the cache or use a different browser for this test.

Custom Template test

templatetest.py is used to make sure custom template files are loaded properly.

Expected output:

This should simply add Template Test at the top of the page, before the standard test suite. Output should print at the bottom of the page.

Detect tests

detect_test.py tests two routes to ensure functions and scripts are detected properly.

Function detection

autofront.add(detect_function)

Expected output

This is a function

Script detection

autofront.add('detect_script.py')

Expected output

This is a script

Clone this wiki locally