Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web Development with Julia and Genie

You're reading from   Web Development with Julia and Genie A hands-on guide to high-performance server-side web development with the Julia programming language

Arrow left icon
Product type Paperback
Published in Nov 2022
Publisher Packt
ISBN-13 9781801811132
Length 254 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ivo Balbaert Ivo Balbaert
Author Profile Icon Ivo Balbaert
Ivo Balbaert
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Part 1: Developing Web Apps with Julia
2. Chapter 1: Julia Programming Overview FREE CHAPTER 3. Chapter 2: Using Julia Standard Web Packages 4. Chapter 3: Applying Julia in Various Use Cases on the Web 5. Part 2: Using the Genie Rapid Web Development Framework
6. Chapter 4: Building an MVC ToDo App 7. Chapter 5: Adding a REST API 8. Chapter 6: Deploying Genie Apps in Production 9. Chapter 7: Adding Authentication to Our App 10. Chapter 8: Developing Interactive Data Dashboards with Genie 11. Index 12. Other Books You May Enjoy

Working with Julia

In this section, we will set up a standard Julia development environment, and learn how to work with Julia scripts as well as the Read–Eval–Print Loop (REPL). The REPL allows you to work with Julia in an interactive way, trying out expressions, function calls, and even executing whole programs.

You’re good to go when typing in julia at the terminal starts up the REPL:

Figure 1.1 – The Julia REPL

Figure 1.1 – The Julia REPL

Using the REPL to use Julia interactively

Try typing in 256^2, giving the result 65536, or rand(), which gives you a random number between 0 and 1, for example, 0.02925477322848513. We’ll use the REPL extensively throughout the book, and the Genie web framework discussed in Part 2 allows you to build your entire web app from the REPL.

Some useful REPL keyboard shortcuts you will use often include the following:

  • Ctrl + D: To exit the REPL
  • Ctrl + L: To clear the screen
  • Ctrl + C: To get a new prompt
  • Up and down arrows: To reuse recent commands

To see which folder you are in, type pwd(). To change the current folder, execute cd("path/to/folder").

A feature we’ll use a lot in Genie is creating new files from within the REPL with the touch command. For example, to create an empty testset.jl file in an existing folder structure, testing/phase1, enter the following:

julia> touch(joinpath("testing", "phase1", "testset.jl"))

The REPL returns testing\\phase1\\testset.jl on Windows and testing/phase1/testset.jl on *nix systems.

The joinpath function constructs the directory path starting in the current folder, and touch creates the file.

Using the package mode to jump-start a project

The Julia ecosystem encompasses thousands of libraries, called packages (see https://julialang.org/packages/), for which Julia has a built-in package manager, Pkg.

The REPL has a special mode for working with packages, which is started by typing ] at julia> prompt, which brings you to package mode: (@v1.8) pkg>.

Some useful commands in this mode to type in after the pkg> prompt are as follows:

  • st or status: Gets a list of all the packages installed in your environment.
  • add PackageName: Adds a new package (you can add several packages separated by , if needed).
  • up or update: Updates all your packages.
  • up or update PackageName: Updates a specific package.
  • activate .: Activates the current project environment (see the Packages and projects section under Using Julia modules and packages). rm or remove PackageName: Removes a specific package.
  • ?: Lists all available commands.
  • The backspace key: Exits the pkg> mode.

In the next section, Parsing a CSV file, we will work with a comma-separated values (CSV) file of to-do items. The CSV package can be imported and set up to be used in your Julia REPL by typing the following:

julia> using Pkg
julia> Pkg.add("CSV")
julia> using CSV

The last line of the preceding command brings the definitions of the CSV package into scope.

Alternatively, from the package mode, use the following:

]:
(@v1.8) pkg> add CSV

The preceding command installs all packages CSV depends on and then precompiles them. This way, the project gets a jump-start, because the just-in-time (JIT) compiler doesn’t have to do this work anymore.

Using Julia with the VS Code plugin

Julia code can also be saved and edited in files with a .jl extension. Numerous IDEs exist to do that. In this book, we’ll use the VS Code platform with the excellent Julia plugin, which provides syntax highlighting and completion, lookup definitions, and plotting among many other features.

A handy way to start VS Code from the terminal prompt is by typing in code.

Search in the Extensions tab for Julia and install it. Then, open up a new file, and type in println("Hi Web World from Julia!"), and save it as hiweb.jl.

Run the program in VS Code with F5 to see the string printed out. Or start the REPL and type the following to get the same result:

julia> include("hiweb.jl")

include evaluates the contents of the input source file.

Or, from a terminal prompt, execute a Julia source file simply by typing the following:

julia hiweb.jl

In all cases, the output will be Hi Web World from Julia!

The include command loads in the Julia file and executes the code.

Continuing the example from the previous section, if you want to start editing the newly created testset.jl file in VS Code when you are working in the REPL, simply type the following:

 julia> edit(joinpath("testing", "phase1", "testset.jl"))

Now that you have some idea of working with Julia using the REPL, in package mode, and using VS Code, let us dig deeper into understanding the basics of the language. In the next section, we will explore some of the basic types, flow controls, functions, and methods in Julia.

You have been reading a chapter from
Web Development with Julia and Genie
Published in: Nov 2022
Publisher: Packt
ISBN-13: 9781801811132
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image