Building a Motion Application
In this tutorial, we'll go through setting up an application of Motion components. We'll go through setting up a simple server with custom components and then demonstrate how to interact with it using Axios in a TypeScript environment.
Prerequisites
Step 1: Define Some Components
We'll write two components: one representing a counter, and another representing a calculator. The counter will increment a count every time an operation is called. The calculator will add and subtract two numbers.
sample_components.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Step 2: Create an Application
We'll create an application that serves the two components we just defined.
app.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
In the script, we include a statement to print the secret key (which is useful if you don't specify your secret key and Motion automatically creates one for your application). Keep this key safe, as it is used to authenticate requests to the application.
To run the application, we can run python app.py
in the terminal. We can also run uvicorn app:fastapi_app --reload
to run the application with hot reloading.
One of the powerful features of FastAPI is its automatic generation of interactive API documentation. Once your server is running, you can access the API documentation by visiting http://localhost:8000/docs
in your web browser. This documentation provides a detailed overview of all the available routes (i.e., the Counter and Calculator component flows) and allows you to directly test the API endpoints from the browser.
Step 3: Interact with the Application
Suppose we are in TypeScript and want to interact with the application. We can use Axios to make requests to the application. First, install Axios in your web application:
npm install axios
Then, we can write a simple Typescript function to make requests to the application:
queryMotionApp.ts | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Since the application is served as a REST API, we can also use any other HTTP client to interact with the application.