Engee documentation
Notebook

.jl files: application scenarios and features

This review is about .jl files. Specifically, such applications as:

  • creating custom modules
  • using certain scripts
  • code debugging
  • running server applications

Creating custom modules

Unlike .ngscript files , files with the extension .jl can be used to create custom modules.

Let's consider 2 files.

  1. MyModule.jl
  2. _MyScriptModule.ngscript

Both files include modules with the same content: MyModule and MyScriptModule respectively. But for the case of .jl-file we can connect the module using:

In [ ]:
include("MyModule.jl")
using .MyModule

area(Rectangle(2,3))

But using .ngscript, it is no longer possible to connect the module:

In [ ]:
try
    include("_MyScriptModule.ngscript")
    using .MyScriptModule

    area(Rectangle(2,3))
catch e 
    println(e)
end
LoadError("/user/start/examples/project_management/jl_files_usage/_MyScriptModule.ngscript", 1, ErrorException("syntax: { } vector syntax is discontinued around /user/start/examples/project_management/jl_files_usage/_MyScriptModule.ngscript:1"))

Automating the execution of actions

Let's imagine that there is a table of students and their repositories with homework assignments.

| Surname | I. | O. | Git repository | | ------- | --- | --- | --------------------------------------- | | Ivanov | I. | I. | git@github.com:Ivanov/Julia_course.git | git@github.com:Ivanov/Julia_course.git | | Pavlov | P. | P. | | | | Petrov | P. | P. | P. | git@github.com:Petrov/Julia_course.git | | Sidorov | I. | P. | git@github.com:Sidorov/Julia_course.git | git@github.com:Sidorov/Julia_course.git |

A teacher teaches a subject to several groups at the same time.


Then, using .jl files, you can check homework assignments, by copying all the files automatically to the same directory where the corresponding .jl file is located.


Note that there are no folders in the directory now.

Let's recreate this situation by executing the cell below. After executing the cell code, the folders will appear in your directory:

jl-files
├─── Ivanov
│ └─── Seminar_1
├─── Petrov
│ └─── Seminar_1
└──── Sidorov
    └─── Seminar_1

A description to what is being executed will be given in the next cell.

In [ ]:
run(`bash -c """

julia generate_student_folders.jl students_group_1.csv Семинар_1

"""`)
ARGS = ["students_group_1.csv", "Семинар_1"]
df = 4×4 DataFrame
 Row │ Фамилия   И.       О.       GitHub
     │ String15  String3  String3  String?
─────┼───────────────────────────────────────────────────────────────
   1 │ Иванов    И.       И.       git@github.com:Ivanov/Julia_cour…
   2 │ Павлов    П.       П.       missing
   3 │ Петров    П.       П.       git@github.com:Petrov/Julia_cour…
   4 │ Сидоров   И.       П.       git@github.com:Sidorov/Julia_cou…
Out[0]:
Process(`bash -c '

julia generate_student_folders.jl students_group_1.csv Семинар_1

'`, ProcessExited(0))
  • The function run can run commands.
  • bash -c means "execute command in the bash shell".
  • julia - juila программа.jl аргумент_1 аргумент_2 ...
  • generate_student_folders.jl - file that creates folders and clones student repositories into them
    • ARGS[1] - name of the file containing the data (students_group_1.csv)
    • ARGS[2] - name of the class/work to check(Семинар_1)
Why not replace the generate_student_folders.jl script with a similar function?

The convenience of using the script is that within it, unlike the function, you can use using and import.

That is, we can call a separate process $\rightarrow$, connect packages $\rightarrow$ in it, execute actions $\rightarrow$ and return back to our main programme.

Note that we used the package CSV inside the programme generate_student_folders.jl, but this package is not connected in our script.

In [ ]:
try
    df = CSV.read("students_group_1.csv", DataFrame);
catch e 
    sprint(showerror,e)
end
Out[0]:
"UndefVarError: `CSV` not defined"

Code debugging

Although code debugging is also possible in scripts, .jl files have an advantage over them. And here's why:

.ngscript represents a json-file.

For example, a _sqrt_16.ngscript file is a json-file, inside of which there is

# part of the file about the first text cell

      "id": "ffe36da1",
      "cell_type": "markdown",
      }, "source": [
        "Value $\\sqrt{16}$"
      ],
# part of the code about the first code cell

      "id": "a4b6a26f",
      "cell_type": "code",
      "source": [
        "sqrt(16)"
      ],

So, debugging such a file is actually a non-trivial task. Let's make sure of that

Debugging .ngscript files

Let's create the function circle_area in the current .ngscript-file.

In [ ]:
"Площадь круга по радиусу"
function circle_area(r)
  π*r^2
end
Out[0]:
circle_area

Using @functionloc we can find out the name of the file and the string of the method to be called.

In [ ]:
@functionloc circle_area(2)
Out[0]:
(nothing, 2)

As we can see, the file name is not returned, and the string is the 2nd line of the cell (after the description of our function "Circle area by radius").

Debugging .jl-files

However, there is no such problem for .jl-files. And the position of the function area declared in the file MyModule.jl is printed quite clearly:

In [ ]:
@functionloc area(Rectangle(2,3))
Out[0]:
("/user/jl-files/MyModule.jl", 17)

otladka.png

.jl-files as part of applications

To develop applications using, for example, Genie, it is .jl files, not scripts, that need to be created inside the application directory.

Thus, the main file of the application should be a file called app.jl.