Building Frontend
In Morph, we use Markdown to build the frontend. By placing components in Markdown files, you can build the UI of your web application.
Development Flow
Here’s the process for frontend development in Morph:
Create an .mdx file
Create a .mdx
file in the src/pages
directory to create a page in your web application. The URL is generated based on the file path.
Add page title
Add a title to your .mdx
file. The first heading in the page is treated as the title.
Place components
Refer to the Component Reference while placing components suitable for your data app.
Start development server
Start the development server to preview your web application locally.
File-based Routing
The application’s URLs are generated based on the mdx file paths.
Examples:
src/pages/new-page-1.mdx
->/new-page-1
src/pages/new-page-2/index.mdx
->/new-page-2
src/pages/new-page-3/sub-page.mdx
->/new-page-3/sub-page
Additionally, the following are reserved words with special meanings:
-
src/pages/_app.tsx
You can set up global layouts. Child routes are placed in<Outlet>
. -
404.tsx
You can set up a global 404 page.
State Management
When developing the frontend, you may want to save user input or selected values as state and send them as variables to the server. In such cases, follow the guide below for implementation.
Declaring State: defineState()
Use defineState()
to declare state. defineState()
takes an object as an argument and sets it as the initial value.
Accessing State Values: .value
You can access the values of declared state by calling the .value
property as follows:
Tips
If you find it tedious to specify the variable name declared with defineState()
every time like in this example, you can shorten it by destructuring the variables.
Updating State: .update()
Use the update()
method to update state.
Using Input Components
The Morph framework provides components for reflecting user input to states declared with defineState()
.
For more details, refer to Input Components.
Using input components makes it easy to reflect user input to state.
Passing Values to Data Components
Data components can accept a variables
property.
By passing the values of states declared with defineState()
to this property, you can pass user input values to Python functions or SQL files.
[Advanced] Using Data API
Getting Execution Results: loadData()
You can use the loadData()
function to get execution results from Python or SQL.
Specify the name of Python or SQL.
Specify the format of data to retrieve.
Variables object to pass to Python or SQL.
You can also pass State
defined with defineState()
.
When State
is passed, data is retrieved again when the State
value changes.
A QueryState<T>
object is returned.