attheoaks.com

Creating Fullstack One-Page Web Applications Using Julia

Written on

Introduction to Toolips Tutorial

In our previous Toolips tutorial, we covered fundamental concepts like managing arguments and writing to Connections. While this is useful for creating APIs and endpoints, it doesn't cater to the development of web-based applications. Toolips operates through a core package, Toolips.jl, which functions like a USB hub, allowing us to enhance its capabilities effortlessly by adding various extensions. Today, we will utilize the Session extension from ToolipsSession, enabling full-stack interactivity through the on method and various Modifiers.

Starting Your Project

To initiate our project, we can use either the new_app or new_webapp method, similar to our last session. The key difference is that new_webapp automatically integrates the Session extension, along with the Files extension from Toolips and a public directory for file routing. In contrast, new_app is typically suited for static sites. For our current project, we will choose the new_webapp method, which takes a project name as a positional argument.

Upon creating our new project directory, we will import the necessary packages:

using Toolips

using ToolipsSession

Additionally, the Files and Session extensions will be included in our server extensions' Vector:

extensions = Vector{ServerExtension}([Logger(), Files(), Session()])

Starting the Web Server

The start function initializes the WebServer as follows:

function start(IP::String = "127.0.0.1", PORT::Integer = 8000)

ws = WebServer(IP, PORT, routes = routes, extensions = extensions)

ws.start()

end

We will also find ToolipsSession included in our Project.toml file. Although we will not need any additional extensions for this project, I would like to briefly outline how to add them. Navigate to your project directory (where Project.toml is located) using Julia's cd method, bash, or the Julia shell REPL. Once in the correct folder, enter the Pkg REPL by pressing ]. After that, type activate . and add your desired extension using the add command.

pkg> add ToolipsMarkdown

Understanding Extensions

Extensions are categorized under ServerExtensions, which can accommodate various extensions themselves. You can access more information by using the ? command:

help?> ToolipsSession

Created in June 2022 by the Chifi team, ToolipsSession is an open-source module that enables interactivity on web pages simply by integrating the Session extension into your ServerTemplate.

Example Setup

To illustrate the functionality of ToolipsSession, we will begin by editing our home function, currently set to a default hello world application:

function home(c::Connection)

end

Next, we'll craft a body for this function:

function home(c::Connection)

ourbody = body("ourbody")

end

We can create a centered div and a greeting heading:

newdiv = div("newdiv", align = "center")

newheading = h("newheading", 2, text = "Hello there!")

Now, we will add a button that will change the alignment whenever clicked:

newbutton = button("newbutton", text = "click me to change alignment")

We need to establish an event connection with the button to perform the alignment change using the on method:

on(c, newbutton, "click") do cm::ComponentModifier

cm[newdiv] = "align" => "right"

end

Finally, we will push the components into our body and write this to the Connection:

function home(c::Connection)

ourbody = body("ourbody")

newdiv = div("newdiv", align = "center")

newheading = h("newheading", 2, text = "Hello there!")

newbutton = button("newbutton", text = "click me to change alignment")

on(c, newbutton, "click") do cm::ComponentModifier

cm[newdiv] = "align" => "right"

end

push!(newdiv, newheading, newbutton)

push!(ourbody, newdiv)

write!(c, ourbody)

end

Using the Development Server

Next, we will include dev.jl to start our development server. When the button is clicked, it will change the alignment of the div. You can even track the alignment state by using getindex to retrieve the property directly.

Additional Styling Options

We can further enhance our components by modifying their styles. For instance, we can set the heading's color to red:

style!(newheading, "color" => "red")

We can even implement a transition effect for a smoother experience:

style!(newheading, "color" => "red", "transition" => "5s")

In our next tutorial, we will create a simple website called DarkMode.jl, where we will explore two new packages: ToolipsMarkdown and ToolipsDefaults.

YouTube Video Insights

To deepen your understanding, check out the following video:

JuliaCon 2020 | Write a WebApp in Julia with Dashboards.jl | Dhairya Gandhi

This presentation covers how to build a web application using Julia's Dashboards.jl framework.

Another useful video is:

Create a web app with Genie Builder GUI - Julia Tutorial

This tutorial walks you through building a web application using the Genie Builder GUI in Julia.

Conclusion

Thank you for engaging with this tutorial! I appreciate your support in my projects and creations.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

# Fascinating Insights: 21 Uncommon Facts to Discover Before 2021

Discover 21 intriguing facts that will make you stand out before the New Year, from scientific phenomena to unexpected trivia.

The Surprising Truth About Life Advice You Might Ignore

Discover the unexpected yet liberating piece of advice that can change your perspective on life and success.

generate a new title here, between 50 to 60 characters long

Shou Zi Chew's testimony before Congress raised concerns about TikTok's ties to China and national security risks, leading to intense scrutiny.

Mindful Eating: Nourishment for Body and Soul

Explore the spiritual significance of mindful eating and its connection to gratitude and nourishment for both body and soul.

Mastering Medium: Your Ultimate Guide to Going Viral and Earning

Discover how to effectively leverage Medium for income and virality through consistency and engagement.

How to Transform Your Life: Epigenetics and the Power of Healing

Explore how epigenetics can guide your healing journey and enhance your well-being through emotional and environmental shifts.

Finding Worth in the Unforeseen: Insights in a Transforming World

Explore the evolving value of everyday items and literature in a world of change.

# Understanding the Enigma of Regular Ron: A Tale of Relationships

Explore the perplexing charm of an average man who attracts stunning women, unraveling the mystery behind his unexpected success.