Showing posts with label racket scheme. Show all posts
Showing posts with label racket scheme. Show all posts

Friday, September 3, 2010

DrRacket: a first attempt at a web server

This is my first attempt at  a web server using DrRacket. It is supposed to calculate VAT given an input of Net or VAT. It doesn't actually do any calculations, but it does respond to buttons and suchlike.

#lang racket

(require web-server/formlets
         web-server/servlet
         web-server/servlet-env)

(define init-input-string (λ(val)
                            (to-string
                             (required
                              (text-input #:value
                                          (string->bytes/locale
                                           val))))))



#|

(define vat-page%
  (class object%
    (super-new)
    (init-field (rate "17.5") (amount "0"))
   
   
    (define vat-formlet
      (formlet
       (div "Amount: " ,{input-string . => . amount}
            (p "")
            "VAT Rate: " ,{(init-input-string "17.5") . => . rate}
            (p "Still in form. Hello Brooks."))
       #t))

   
    (define (button-clicked request)
      ;(define-values (amount rate)
        (formlet-process vat-formlet request)
      (print "insert-post-handle")
      (print amount)
      (render (redirect/get))      )
   
    (define/public (render request)
      `(html (head (title "My Calculator"))
             (body
              (h1 "Simple VAT calculator")
              (form ([action ,(make-url button-clicked)])
                    ,@(formlet-display vat-formlet)
                    (input [(type "submit") (value "Calculate")]))
              (p ,amount)
              (p ,rate))))))

(define (render-vat-page-new request)
  (define vat-page (make-object vat-page%))
  (define (response-generator make-url)
    (send vat-page render make-url))
  (send/suspend/dispatch response-generator))

(serve/servlet render-vat-page-new #:port 80 #:listen-ip #f)

|#



(define vat-formlet
      (formlet
       (div "Amount: " ,{input-string . => . amount}
            (p "")
            "VAT Rate: " ,{(init-input-string "17.5") . => . rate}
            (p "Still in form. Hello Brooks."))
       (values amount rate)))

(define (render-vat-page amount rate request)
  (local [(define (response-generator make-url)
            `(html (head (title "My Calculator"))
                   (body
                    (h1 "Simple VAT calculator")
                    (form ([action
                            ,(make-url button-clicked)])
                          ,@(formlet-display vat-formlet)
                          (input [(type "submit") (value "Calculate")]))
                    (p ,amount)
                    (p ,rate))))
         
          (define (button-clicked request)
            (define-values (amount rate)
              (formlet-process vat-formlet request))
            (print "insert-post-handle")
            (print amount)
            (render-vat-page amount rate (redirect/get)))]
   
    (send/suspend/dispatch response-generator)))


(serve/servlet (λ(request) (render-vat-page "0.0" "17.5" request)) #:port 80 #:listen-ip #f)


Wednesday, August 25, 2010

Creating tables in Racket webserver

I'm starting a journey into creating websites using DrRacket. Sometimes its documentation is a little opaque. If you're looking to create tables that look like this:
then you can do it using the following code:
#lang web-server/insta

(define (start request)
  `(html (head (title "Table test"))
         (body
         
          (h1 "Example 1")
          (p "The most basic of tables:")
          (table (tbody
                  (tr (td "one") (td "two"))
                  (tr (td "three") (td "four"))))
         
          (h1 "Example2")
          (p "Let's add some attributes: a table border, some cell spacing, and a width and background colour to one of the cells:")
          (table ((border "1") (cellspacing "10"))                 
                 (tbody
                  (tr (td ((width "100") (bgcolor "#FF6699")) "one") (td "two"))
                  (tr (td "three") (td "four"))))
            
         
          )))

Tuesday, July 27, 2010

DrRacket: Obtaining the contents of the clipboard

It's as simple as doing this:
(send the-clipboard get-clipboard-string 0)