From: ·········@gmail.com
Subject: Naive question about lisp web applications
Date: 
Message-ID: <1146678188.749010.103100@v46g2000cwv.googlegroups.com>
Please forgive me as I ask what is undoubtedly an extremely naive set
of questions about lisp and the web.

I learned lisp almost 20 years ago, in the pre-web era. I have used
lisp pretty consistently for the last 20 years, but always for
scientific research and almost always for my own personal use
exclusively. I typically write programs to support my research on
non-programming topics (often involving string similarity and sequence
comparison). All of my programs have simple command line user
interfaces.

I would like to be able to access some of the functionality of my
rather large library of lisp programs from a web browser, but I really
don't have the first idea how to do so. So what I need is pointers or
detailed suggestions about how to create simple browser-based
interfaces to my lisp programs.

These days I mostly use CLISP (with slime) under cygwin on a Windows
machine, but I also have easy access to clisp and cmucl on a Linux
machine.

Unfortunately, I know next to nothing about html, java, etc. Can anyone
help me with some example code or good tutorials or even just a
conceptual description of the necessary steps.

I'd like to start with something really simple. For example, I have
lots of functions that compute the similarity between two strings. I'd
like to create a simple web interface to these functions. It would have
two text boxes, one submit button, and a box to display the resulting
similarity score.

Thanks.

-bruce

From: Frank Buss
Subject: Re: Naive question about lisp web applications
Date: 
Message-ID: <j174651p6lj$.6zm3g9eznht8$.dlg@40tude.net>
·········@gmail.com wrote:

> I'd like to start with something really simple. For example, I have
> lots of functions that compute the similarity between two strings. I'd
> like to create a simple web interface to these functions. It would have
> two text boxes, one submit button, and a box to display the resulting
> similarity score.

Maybe this can help you:

http://www.frank-buss.de/lisp/tbnl.html

Let me know, if it works with your Lisp implementation or if you have
problems.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Pascal Bourguignon
Subject: Re: Naive question about lisp web applications
Date: 
Message-ID: <87odyepz7d.fsf@thalassa.informatimago.com>
·········@gmail.com writes:

> Please forgive me as I ask what is undoubtedly an extremely naive set
> of questions about lisp and the web.
>
> I learned lisp almost 20 years ago, in the pre-web era. I have used
> lisp pretty consistently for the last 20 years, but always for
> scientific research and almost always for my own personal use
> exclusively. I typically write programs to support my research on
> non-programming topics (often involving string similarity and sequence
> comparison). All of my programs have simple command line user
> interfaces.
>
> I would like to be able to access some of the functionality of my
> rather large library of lisp programs from a web browser, but I really
> don't have the first idea how to do so. So what I need is pointers or
> detailed suggestions about how to create simple browser-based
> interfaces to my lisp programs.
>
> These days I mostly use CLISP (with slime) under cygwin on a Windows
> machine, but I also have easy access to clisp and cmucl on a Linux
> machine.
>
> Unfortunately, I know next to nothing about html, java, etc. Can anyone
> help me with some example code or good tutorials or even just a
> conceptual description of the necessary steps.
>
> I'd like to start with something really simple. For example, I have
> lots of functions that compute the similarity between two strings. I'd
> like to create a simple web interface to these functions. It would have
> two text boxes, one submit button, and a box to display the resulting
> similarity score.

Writing CGI is very simple.  You just need to know a few HTML.
There are a lot of tutorials, or browse w3.org or the web to learn about HTML.
http://www.w3.org/TR/html4/#minitoc


Have a look at my cookie.cgi example:
http://groups.google.com/group/comp.lang.lisp/msg/bacdf93b5f9fa453?hl=en&

You only need to put it in a CGI directory in your http server,
removing the colon in the first format:

                   (format nil "~:{~A=\"~A\"~^ ~}" (cdr tag-attrs))
-->
                   (format nil "~{~A=\"~A\"~^ ~}" (cdr tag-attrs))

Here is an example of form:

------------------------------------------------------------------------
#!/usr/local/bin/clisp -q -ansi -norc 

# To debug from the shell, add the following options to the #! line:
#    -on-error debug
# and invoke the script as:
# QUERY_STRING='op=div&arg1=43123&arg2=123' ./example-form.cgi 

# Otherwise, just point your browser to http://localhost/cgi/example-form.cgi
# or wherever you install it.


(defmacro h (tag-attrs &rest body)
  (let ((tag   (if (consp tag-attrs)
                   (car tag-attrs)
                   tag-attrs ))
        (attrs (if (consp tag-attrs)
                   (format nil "~{~A=\"~A\"~^ ~}" (cdr tag-attrs))
                   "")))
    `(unwind-protect
          (progn (format t "~&<~A ~A>" ',tag ',attrs)
                 ,@body)
       (format t "</~A>" ',tag))))

(defun formular ()
  (h (form :action "example-form.cgi" :method "get")
     (h br)(h (select :name "op")
              (h (option :value "add" :label "Add" :selected :selected)
                 (princ "Sum"))
              (h (option :value "sub" :label "Subtract")
                 (princ "Difference"))
              (h (option :value "mul" :label "Multiply")
                 (princ "Product"))
              (h (option :value "div" :label "Divide")
                 (princ "Division")))
     (h br)(h (input :type "text" :name "arg1" :size 60 :maxlength 60))
     (h br)(h (input :type "text" :name "arg2" :size 60 :maxlength 60))
     (h br)(h (button :type "submit")
              (princ "Execute"))))

(defun formular-page ()
  (h html
     (h head
        (h title (princ "form test cgi")))
     (h (body bgcolor \#4499ff)
        (h h1 (princ "Input parameters"))
        (formular))))


(defun split-string (string separator)
  (loop 
     :for start = 0 :then (1+ sepos)
     :for sepos = (position separator string :start start)
     :collect (subseq string start (and (< start (length string)) sepos))
     :while sepos))

(defun decode-query (query-string)
  (mapcar (lambda (arg) (split-string arg #\=))
           (split-string query-string #\&)))


(defparameter *functions* 
  (let ((table (make-hash-table :test (function equal))))
    (setf (gethash "add" table) (cons (function +) (quote +))
          (gethash "sub" table) (cons (function -) (quote -))
          (gethash "mul" table) (cons (function *) (quote *))
          (gethash "div" table) (cons (function /) (quote /)))
    table))

(defun argument (name query)
  (or (second (assoc name query :test (function equal))) ""))

(defun process (query)
  (h h1 (princ "Results"))
  (let ((op   (gethash (argument "op" query) *functions*))
        (arg1 (let ((*read-eval* nil))
                (read-from-string (argument "arg1" query))))
        (arg2 (let ((*read-eval* nil))
                (read-from-string (argument "arg2" query)))))
    (h p 
       (if (and op (numberp arg1) (numberp arg2))
           (format t "~A ~A ~A = ~A" 
                   arg1 (cdr op) arg2 
                   (funcall (car op) arg1 arg2))
           (format t "Invalid or missing operator or arguments.")))))

(defun process-page (query)
  (h html
     (h head
        (h title (princ "results")))
     (h (body bgcolor \#4499ff)
        (process query)
        (h h1 (princ "New operation"))
        (formular))))

(format t "Content-type: text/html~2%")
(handler-case
    (let ((query (ext:getenv "QUERY_STRING")))
      (if (and query (< 0 (length query)))
          (process-page (decode-query query))
          (formular-page)))
  (error (err)
    (h (body bgcolor \#ff9944)
       (h h1 (princ "Some Error occured"))
       (h pre (princ err)))))

(ext:exit 0) 
------------------------------------------------------------------------


Of course, you will reach the limits of CGI very quick: everytime you
click on a CGI link or button, your whole clisp script/program is
launched again, which may take time for big programs (mind
ext:saveinitmem :execute t), and which doesn't keep state in the
"server".  Then you'll be ready to explore more sophisticated
solutions, like Araneida or UCW.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This is a signature virus.  Add me to your signature and help me to live.
From: Paolo Amoroso
Subject: Re: Naive question about lisp web applications
Date: 
Message-ID: <8764kmuki2.fsf@plato.moon.paoloamoroso.it>
·········@gmail.com writes:

> I would like to be able to access some of the functionality of my
> rather large library of lisp programs from a web browser, but I really
> don't have the first idea how to do so. So what I need is pointers or

This book includes many examples of web programming:

  Practical Common Lisp
  http://www.gigamonkeys.com/book


Paolo
-- 
Why Lisp? http://wiki.alu.org/RtL%20Highlight%20Film
The Common Lisp Directory: http://www.cl-user.net
From: ·········@gmail.com
Subject: Re: Naive question about lisp web applications
Date: 
Message-ID: <1146761087.127988.94350@e56g2000cwe.googlegroups.com>
The reference to Practical Common Lisp was precisely what I was looking
for. I knew of this book, but I did not realize it contained the
tutorial I needed. Thanks Paolo, and thanks to the others who
responded.

-bruce
http://www.uic.edu/~lambertb