Engee documentation
Notebook

.jl files: application scenarios and features

The review is dedicated to .jl files. Namely, applications such as:

  • creation of custom modules
  • using certain scenarios
  • debugging the code
  • launching server applications

Creating custom modules

Unlike files .ngscript, files with the extension .jl you can use it to create your own modules.

Consider 2 files.

  1. MyModule.jl
  2. _MyScriptModule.ngscript

Both files include modules with the same contents.: MyModule and MyScriptModule accordingly.
But for the case of .jl-then we can connect the module using:

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

area(Rectangle(2,3))

But, using .ngscript, it will no longer be 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

Imagine that there is a table of students and their repositories with homework.

Last name And . O. Git repository
Ivanov I. And. git@github.com:Ivanov/Julia_course.git
Pavlov P. P.
Petrov P. P. git@github.com:Petrov/Julia_course.git
Sidorov I. P. git@github.com:Sidorov/Julia_course.git

The teacher teaches a certain subject in several groups at once.


Then, using .jl-Files can be checked for homework
by copying all files automatically to the directory
where the corresponding file is located. .jl- the file.


Please note that there are no folders in the directory right now.

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

jl-files
├── Ivanov
,── Seminar_1
,── Petrov
,── Seminar_1
,── Sidorov
    ,── Seminar_1
`` A
description of what is being performed 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))
  • Function run It can run commands.
  • bash -c means "execute command in bash shell".
  • julia - juila программа.jl аргумент_1 аргумент_2 ...
  • generate_student_folders.jl - a file that creates folders and clones student repositories in them
    • ARGS[1] - the name of the file containing the data (students_group_1.csv)
    • ARGS[2] - the name of the activity/job that needs to be checked(Семинар_1)
Why not replace the script generate_student_folders.jl a similar function?

The convenience of using a script is that inside it, unlike функции , you can use using and import.

That is, we can call a separate process. connect packages in it perform actions and go back to our main program.

Note that we have used the package CSV inside the program 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"

Debugging the code

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

.ngscript represents json- the file.

For example, the file _sqrt_16.ngscript represents json-a file that contains

# the 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)"
      ],

That is, in fact, debugging such a file is not a trivial task.
Let's make sure of that.

Debugging .ngscript files

Creating a function circle_area in the current .ngscript- the file.

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

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

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

As you can see, the file name has not been returned, and the 2nd row of the cell is indicated as the row (after describing our function "The area is steep in radius").

Debugging .jl-files

However, for .jl- there is no such problem in the files. And the position declared in the file MyModule.jl functions area it is deduced quite definitely:

In [ ]:
@functionloc area(Rectangle(2,3))
Out[0]:
("/user/jl-files/MyModule.jl", 17)
отладка.png

.jl-files as part of applications

To develop applications using, for example, Genie, it is necessary to create exactly inside the application directory .jl files, not scripts.

So, the main file of the application should be a file with the name app.jl.