The magazine of the Melbourne PC User Group

A Site On The World Wide Reb
Trevor Gosbell
 

Trevor Gosbell gives us the third in this series of articles on REBOL — this month we learn about the REBOL Distributed Desktop


The World Wide Reb is the tongue-in-cheek name given to the network of distributed REBOL applications accessible from the REBOL/View desktop - more formally it is known as the REBOL Distributed Desktop. Once you've set up a rebsite you can add it to Distributed Desktop and make it available to all REBOL/View users.

The REBOL folks reckon that setting up a rebsite is easier than setting up an ordinary HTML Web page. Let's put that claim to the test.

A Rebol Mini-Application For the Internet

Because of the fact that users browse the World Wide Reb using their REBOL/View desktop, we'll need some type of Internet-enabled application. The file renamer we developed in the previous articles is not really suitable as it is intended for use only on the local machine. Also REBOL has built-in security that stops its own programs from accessing the user's computer without permission. I don't want my rebsite asking visitors for access to files on their computers - I certainly wouldn't like it!

No, it must be a network-oriented application that doesn't need access to files on the user's computer.

The "Melbourne Weather" Application

Recently I had the dubious privilege of working in a basement level office that lacked any natural light, let alone a window. Before leaving the office we always wanted to know what conditions were like up on the surface. Rather than climbing the stairs to the nearest window we would hit the Bureau of Meteorology Web site and check the latest observations at http://www.bom.gov.au/products/IDV65119.shtml.
 



Figure 1. Weather observations on the Bureau of Meteorology Web site.

The trouble with this Web page is that it shows data from a number of observation stations (Figure 1) but all I wanted was Melbourne. Also it includes more observations than I needed - two measures of wind speed? And what is a dew point anyway? Information overload! What I need is a little program that will access this page and tell me the temperature, whether it's windy out and if it has been raining.

There are countless approaches to this task, and a REBOL solution appears in Figure 2. This will be the mini-application to include on the rebsite.

The program (Listing 1 - below, or download here) addresses the two parts to the problem - extracting the required data and presenting the information in an easy-to-read form.

Parsing The Web Page

First we read the Web page into the variable page, then use the REBOL parse command to extract the data. Just as the layout command uses a dialect (the Visual Interface Dialect), the parse command also uses a dialect but this one is for text-processing rather than visual display. The format of the parse command is:

parse some_text [using these rules]  

So the parse dialect allows us to build up a set of text-processing rules, as follows:

parse page [
thru "Last updated: "
copy data_item to "^/" (append data_list data_item) thru "Melbourne</a></td>" thru <td nowrap align="center"> copy data_item to </td> (append data_list data_item) 10 [thru <td align="center"> copy data_item to </td> (append data_list data_item)
]

Or rephrased in English:

  • parse the HTML text stored in the page variable
  • move through the HTML until you find the text "Last updated: "
  • copy everything from here until the end of the line into the variable data_item
  • append the contents of data_item to the empty list data_list
  • move through the HTML until you find the text "Melbourne</a></td>"
  • move through the HTML until you find the tag <td nowrap align="center">
  • copy everything up to the tag </td> and append it to the list data_list
  • repeat the following ten times:
  • move through the list until you find the tag <td align="center">
  • copy everything up to the tag </td> and append it to the list data_list
Now the variable data_list holds a list that includes a date stamp and all of the current observational data for Melbourne (time of observation, temperature, dew point, relative humidity, wind direction, wind speed (km/h and knots), wind gust (km/h and knots), air pressure, rainfall since 9.00 am) - a total of 12 fields. We now have our observations - it's time to present them in a graphical user interface (GUI).

But First...

A quick word about the REBOL path syntax. There are two ways to access each data element in a block such as
data_list. One is to use the ordinals first data_list, second data_list, third data_list, and so on. But the method used here is the REBOL path syntax:
 
data_list/1, data_list/2, data_list/3 - a bit like arrays in other languages.

A GUI Display
 

Looking at the second part of Listing 1, we are familiar with the view layout command combination from the previous article. Next we introduce a backdrop effect - in this case a colour gradient starting as black (0.0.0) in the top left corner to a dark green (0.128.128) at the bottom right. The banner command gives the window a nice big title.

The next line is a doozy - and all it does is make a subheading in the format: "Monday 12 May 2003 at 18:00"!
Let's take it apart:

data_list/1 returns the first item in data_list, which is text in the format "18:12 AEST Monday 12 May 2003" but all I need is the "Monday 12 May 2003" portion.

parse data_list/1 none

Using parse without any text rules (none) breaks the string at each whitespace to make the block: ["18:12" "AEST" "Monday" "12" "May" "2003"]



Figure 2. The REBOL solution.

skip...2 takes the result of the parse function and skips the first two elements, resulting in the block: ["Monday" "12" "May" "2003"]

parse data_list/2 none

parses the second element of data_list (the observation time stamp), a string of the format "12 18:00" into the block ["12" "18:00"]

second selects the second string out of ["12" "18:00"], and returns "18:00"

reform after the above processing - reform gets passed a block like this [["Monday" "12" "May" "2003"] "at" "18:00"], which it reduces and forms into the string "Monday 12 May 2003 at 18:00"

vh1 is a level 1 heading for video display

Phew! Sit down and have a headache pill if you need to. It gets easier from here.

The pad command adds some space (60 pixels at the left and 20 pixels at the top) for the widgets that follow.

The next line uses the rejoin command to link up everything in the block and pass it as a string to
vtext, which is a text field for video display. (Note: #"^(B0)" is the hexadecimal character code for the degree symbol.) And return moves the insertion point back to the left margin set by guide. This pattern repeats until we set a new margin and put in the Done button.

Save the code in Listing 1 to file
melb_weather.r and we have an application that we can run from our own machine (run rebol melb_weather.r or double-click the melb_weather.r) or via the Internet as outlined on later.

Set Up a Rebsite

Remember, this is the easy bit! It should not come as a surprise that you set up a rebsite using a REBOL script (Listing 2 - see below or download here).

The key points to note are:

  • the REBOL header must include the type declaration as shown
  • two values are allowed for text colour the first (255.255.255) makes the icon labels white, the second (128.128.128) makes labels turn grey when the mouse rolls over the icon
  • and backdrop uses the same effect as in melb_weather.r
  • the file command adds the weather reporting application to the page
And that's all there is to it - other files and folders are added in a similar way. The code in Listing 2 is saved as index.r and uploaded to a public Web server along with melb_weather.r. To access the rebsite choose Goto on the REBOL/View desktop and enter the URL eg. http://member.melbpc.org.au/~tgosbell/rebsite/index.r into the text field (Figure 3) and click Goto.
 



Figure 3. Entering the URL in the REBOL/View desktop.

Maybe the REBOL folks are right - it does seem easy.

Rebol Resources On the Web

More information about the REBOL/View Distributed Desktop
http://www.rebol.com/docs/desktop.html

More information on parse can be found at:
http://www.codeconscious.com/rebol/parse-tutorial.html

http://www.rebolforces.com/zine/rzine-1-06.html#sect4
and in the REBOL manual
http://www.rebol.com/docs/core23/rebolcore-15.html.
 

      
REBOL [
Title: "Melbourne Weather Update"
File: %melb_weather.r
Date: "May 2003"
Purpose: "Retrieve current weather observations from
Bureau of Meteorology and display."
]

; first process the web page
page: read http://www.bom.gov.au/products/IDV65119.shtml
data_list: []
parse page [
thru "Last updated: "
copy data_item to "^/" (append data_list data_item)
thru "Melbourne</a></td>"
thru <td nowrap align="center">
copy data_item to </td> (append data_list data_item)
10 [thru <td align="center">
copy data_item to </td> (append data_list data_item)]
]

; second display the information
view layout [
backdrop effect [gradient 0x0 0.0.0 0.128.128]
banner "Melbourne Weather Update"
vh1 reform [skip parse data_list/1 none 2
"at" second parse data_list/2 none ]
pad 60x20
vtext rejoin ["Temperature: " data_list/3 #"^(B0)" "C"]
vtext rejoin ["Relative Humidity: " data_list/5 "%"]
vtext rejoin ["Wind:"data_list/6 " at " data_list/8 "Knots"]
vtext rejoin ["Rain since 9am: " data_list/12 "mm"]
pad 180x20
button " Done " [quit]

]
      
Listing 1. 
 
REBOL [type: 'index]

summary {
This is my rebsite
}

text-color 255.255.255 128.128.128
backdrop [gradient 0x0 0.0.0 0.128.128]
file "Melb Weather" %melb_weather.r info "Get the latest
weather observations for Melbourne"

Listing 2

About The Author
Readers will be relieved to learn that Trevor now occupies an office closer to ground level, where the ankles of passing pedestrians are dimly visible through a small air vent
.

Reprinted from the July 2003 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[ About Melbourne PC User Group ]