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

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1>{{= title }}</h1>
    {{ each items, item }}
        <h2>{{= item.title }}</h2>
        <div class="content">{{- item.content }}</div>
    {{ end }}
    <ul>
        <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>
    </ul>
  </body>
</html>

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.

author
- Paul Brown
version
- 0.1.4
license
- MIT
 reply_404(Options, Request) is det
Arguments:
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
Arguments:
Options- as per http_server/3

Re-exported predicates

The following predicates are exported from this file while their implementation is defined in imported modules or non-module files loaded by this module.

 reply_json_dict(+JSONTerm) is det
 reply_json_dict(+JSONTerm, +Options) is det
As reply_json/1 and reply_json/2, but assumes the new dict based data representation. Note that this is the default if the outer object is a dict. This predicate is needed to serialize a list of objects correctly and provides consistency with http_read_json_dict/2 and friends.
 reply_json_dict(+JSONTerm) is det
 reply_json_dict(+JSONTerm, +Options) is det
As reply_json/1 and reply_json/2, but assumes the new dict based data representation. Note that this is the default if the outer object is a dict. This predicate is needed to serialize a list of objects correctly and provides consistency with http_read_json_dict/2 and friends.

Undocumented predicates

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

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