Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Web Development with Julia and Genie
  • Table Of Contents Toc
Web Development with Julia and Genie

Web Development with Julia and Genie

By : Ivo Balbaert
5 (1)
close
close
Web Development with Julia and Genie

Web Development with Julia and Genie

5 (1)
By: Ivo Balbaert

Overview of this book

Julia’s high-performance and scalability characteristics and its extensive number of packages for visualizing data make it an excellent fit for developing web apps, web services, and web dashboards. The two parts of this book provide complete coverage to build your skills in web development. First, you'll refresh your knowledge of the main concepts in Julia that will further be used in web development. Then, you’ll use Julia’s standard web packages and examine how the building blocks of the web such as TCP-IP, web sockets, HTTP protocol, and so on are implemented in Julia’s standard library. Each topic is discussed and developed into code that you can apply in new projects, from static websites to dashboards. You’ll also understand how to choose the right Julia framework for a project. The second part of the book talks about the Genie framework. You’ll learn how to build a traditional to do app following the MVC design pattern. Next, you’ll add a REST API to this project, including testing and documentation. Later, you’ll explore the various ways of deploying an app in production, including authentication functionality. Finally, you’ll work on an interactive data dashboard, making various chart types and filters. By the end of this book, you’ll be able to build interactive web solutions on a large scale with a Julia-based web framework.
Table of Contents (13 chapters)
close
close
1
Part 1: Developing Web Apps with Julia
5
Part 2: Using the Genie Rapid Web Development Framework

Useful techniques in Julia web development

In this section, we will highlight some methods and techniques that you will see used often in Julia web development and that we will also use in the project in Part 2. Here is a list of these key techniques:

  • Multi-line strings: These strings are delineated with """. These are often useful in web apps to use chunks of HTML code in a variable as follows:
    form = """ <form action="/" method="POST" enctype="multipart/form-data"> <input type="text" name="name" value="" placeholder="What's your name?" /> <input type="submit" value="Greet" /> </form> """
  • String substitution: This can be used to insert variable contents into messages on the screen as follows:
    <h4 class="container">
    Sorry, no results were found for "$(params(:search_movies))"
    </h4>
  • do block syntax: This syntax makes the code easier to read. Here is a simple example:
    list = [1, 2, 3]
    map(x -> x^2, list)

This can also be written as follows:

map(list) -> do x
    x^2
end
  • Here is an example usage from the Genie framework:
    route("/hello.html") do
     html("Hello World")
    end

Here, route() and html() are functions from the Genie framework. The do x syntax creates an anonymous function with argument x and passes it as the first argument to the function stated before do. In this example, the string "Hello World" will be shown on the web page when the /hello.html URL is requested.

Another way to write this, which is clearly not that readable, is as follows:

route(html("Hello World"), "/hello.html")

We will be using this do syntax quite often in Part 2.

  • &&: The Boolean and && operator is often used to write concise conditional code, for example:
    isempty(strip(params(:search_movies)))
    && redirect(:get_movies)

The preceding code first evaluates the isempty(strip(params(:search_movies))) part; if this is false, nothing happens anymore. Only if the first part is true will the second, redirect(:get_movies), get evaluated. Thus, if the search_movies parameter has a value, only then will the redirect to :get_movies take place.

  • ||: The or || operator doesn’t evaluate the part after || when the first part is true, for example:
    isa(getfield(m, field), Int) || return ValidationResult(invalid, :is_int, "should be an int")

In the preceding example, if m is of type Int, the ValidationResult from the right-hand side is not shown.

  • Pipe operator: This operator, denoted by |>, is quite handy. An example is as follows:
    h1("Welcome Admin") |> html

The output of the preceding function before the pipe operator (|>) is given as a first argument to the function after the pipe. This allows for easy function chaining.

  • Ternary form: An if condition a else b end statement is often written in a ternary form: condition ? a : b.

An example is as follows:

flash_has_message() ? """<div class="alert alert-$flashtype alert-dismissable">$(flash())</div>""" : ""

If the function flash_has_message() returns true, then the multiline string after ? (which contains the HTML code for a div) is the result of the expression; if false the empty string "" is returned.

  • Symbols: These are often used in Julia (web) code denoted by :, for example:
    julia> sym = :info
    :info
    julia> typeof(sym)
    Symbol

Symbols are used to indicate access to a variable (such as info), but what info exactly contains is not evaluated at that moment in code. A symbol gets replaced with the value bound to that symbol when the expression containing the symbol is evaluated at runtime.

Here is an example from Genie passing the values of the:user_id and :user_status variables in the payload to a createUser function:

createUser(postpayload(:user_id), postpayload(:user_status, "active"))

So, postpayload(:user_id) can be seen as binding values to variables (something that can change) inside user-provided expressions. Here, on evaluation, :user_id is replaced by the value Symbol is pointing to.

  • <% %>: Web frameworks often embed code inside HTML with <% %>. This can be used in the Julia Genie web framework, as follows:
    <h4><% movie.description %></h4>

In the preceding code, <% %> is used to insert a description field of a movie instance in that place.

<% %> can also contain whole blocks of code as well as function calls.

Embedded code can also contain a call to the @yield macro, like in the following snippet from app.jl.html in Genie:

<body>
<div class="container">
<%
@yield
%>
</div>
</body>

@yield is used to output the content of the view/controller into the layout.

Do familiarize yourself with the techniques described in this section. Doing so will make Julia web code instantly more understandable. All Julia web apps are projects that contain modules and use packages, so that’s what we’ll discuss in the next section.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Web Development with Julia and Genie
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon