Did you know ... Search Documentation:
Pack simple_web -- prolog/sw/simple_web.pl
PublicShow source

Easy, simple websites with Prolog.

Example basic use

Create a directory with app.pl

:- use_module(sw/simple_web).

sw:route(home, '/', _Request) :-
    reply_html("<h1>Hello, world!</h1>
                <img src='static/images/logo.jpg' alt='logo'/>").

sw:route(template, '/test', _Request) :-
    Data = data{ title: 'Hello'
               , items: [ item{ title: 'Item 1', content: 'Abc 1' }
                        , item{ title: 'Item 1', content: 'Abc 2' }
    reply_template(test, Data).

sw:route(termarized, root(termerized), _Request) :-
    reply_html([title('Termerized')], [h1('Termerized Example'), p('With some text')]).

sw:route(api, '/api', method(get), _Request) :-
    reply_json_dict(data{example: "Hello, world!"}).

:- run([port(5000)]).

Inside this root directory, create a folder called static to save your static files, such as your css, javascript and images. Place an image called logo.jpg into static/images/.

Also inside the root directory, create a folder called templates, inside this place test.html

    <h1>{{= title }}</h1>
    {{ each items, item }}
        <h2>{{= item.title }}</h2>
        <div class="content">{{- item.content }}</div>
    {{ end }}
        <li><a href="{{= url_for('home') }}">Home</a></li>
        <li><a href="{{= url_for('termarized') }}">Termarized Example</a></li>
        <li><a href="{{= url_for('api') }}">API Example</a></li>

Finally, run app.pl and navigate to http://localhost:5000

:~$ swipl app.pl

More Examples

A repository of examples, including using static, templates and creating an API can be found in the simple_web_examples repository.

- Paul Brown
- 0.1.4
 reply_404(Options, Request) is det
Options- as per http_404/2. ! reply_404(Request) is det. Respond with a 404, no options, as per http_404/2
 run is det
run the webserver on a random free port
 run(+Options) is det
run the webserver with Options
Options- as per http_server/3

Undocumented predicates

The following predicates are exported, but not or incorrectly documented.

 read_json_dict(Arg1, Arg2)
 read_json_dict(Arg1, Arg2, Arg3)
 reply_json_dict(Arg1, Arg2)
 reply_redirect(Arg1, Arg2, Arg3)
 url_for(Arg1, Arg2)