unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Artanis web REPL
@ 2016-06-24 11:43 Jan Wedekind
  2016-06-24 17:20 ` Nala Ginrut
  0 siblings, 1 reply; 6+ messages in thread
From: Jan Wedekind @ 2016-06-24 11:43 UTC (permalink / raw)
  To: guile-user

Just managed to get a simple web REPL working. It still needs some form of 
safe environments for each browser sessions. Maybe one can do something 
like the IPython notebooks in the future.

Any comments/suggestions welcome.

     ; prototype Guile repl for the browser
     (use-modules (srfi srfi-26) (artanis artanis) (artanis cookie) (artanis utils) (ice-9 regex))

     (init-server)

     (define (dot lst) (apply cons lst))

     (define (quoted expr) (call-with-output-string (cut write expr <>)))

     (define (line-breaks s) (regexp-substitute/global #f "\n" s 'pre "<br/>" 'post))

     (define lines '())

     (define (reset session)
       (set! lines (assoc-set! lines session '()))
       (output session (format #f "; session: ~a~&" session)))

     (define (output session line)
       (assoc-set! lines session (cons line (assoc-ref lines session))))

     (define (repl session line)
       (catch #t
         (lambda ()
           (output session line)
           (let [(result (eval-string line (current-module)))]
             (if (not (unspecified? result))
               (output session (format #f "; ~a~&" (quoted result))))))
         (lambda (key function fmt vals . args)
           (let* [(msg  (apply format #f fmt vals))
                  (info (format #f "; ~a" msg))]
             (output session info)))))

     (define (editor session)
       (tpl->response
         `(html
            (body
              ,(map (lambda (line) `(p ,(line-breaks (eliminate-evil-HTML-entities line)))) (reverse (assoc-ref lines session)))
              (form (@ (action "") (method "post"))
                (input (@ (type "text") (name "line") (autofocus "autofocus"))))))))

     (get "/" #:session 'spawn
       (lambda (rc)
         (let [(session (:session rc 'spawn))]
           (reset session)
           (editor session))))

     (post "/"
       (lambda (rc)
         (let* [(session (cookie-ref  (rc-cookie rc) "sid"))
                (post-data (map dot (generate-kv-from-post-qstr (rc-body rc))))
                (line      (uri-decode (assoc-ref post-data "line")))]
           (repl session line)
           (editor session))))
     (run)



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Artanis web REPL
  2016-06-24 11:43 Artanis web REPL Jan Wedekind
@ 2016-06-24 17:20 ` Nala Ginrut
  2016-06-24 21:40   ` Jan Wedekind
  2016-06-24 21:51   ` Jan Wedekind
  0 siblings, 2 replies; 6+ messages in thread
From: Nala Ginrut @ 2016-06-24 17:20 UTC (permalink / raw)
  To: Jan Wedekind, guile-user

Hi Jan!
First, thanks for sharing this code.
I've tried, it's sweet that I love it!
And I'm looking forward to someone could finish it as a complete
project just like IPython notebook or similar.

On Fri, 2016-06-24 at 12:43 +0100, Jan Wedekind wrote:
> 
     (post "/"
>        (lambda (rc)
>          (let* [(session (cookie-ref  (rc-cookie rc) "sid"))
>                 (post-data (map dot (generate-kv-from-post-qstr (rc-
> body rc))))
>                 (line      (uri-decode (assoc-ref post-data
> "line")))]
>            (repl session line)
>            (editor session))))


It is embarrassing when I'm trying to point out the fancy "shortcut"
for saving code, but finally I realized there're bugs. ;-(
Anyway, they're just syntax sugar for convenience, it won't be harm if
you dismiss it. I'm working on the server core of Artanis, so I may fix
it next release.
But I still put my code here to shed some light. ;-P

=========================code=================================
(post "/" #:from-post #t #:cookie '(name repl)                        
  (lambda (rc)                                                        
    (let [(session (:cookies-ref rc 'repl "sid"))                      
          (line (uri-decode (:from-post rc 'get "line")))]            
      (repl session line)                                              
      (editor session))))
=========================end==================================


Best regards.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Artanis web REPL
  2016-06-24 17:20 ` Nala Ginrut
@ 2016-06-24 21:40   ` Jan Wedekind
  2016-06-24 21:51   ` Jan Wedekind
  1 sibling, 0 replies; 6+ messages in thread
From: Jan Wedekind @ 2016-06-24 21:40 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-user

Cheers for the simplifying the code [1]. That's really useful.
However I only got part of the improvements working (I am using Artanis 
version 0.1.2). When using the following code ...

     (post "/" #:from-post #t #:cookie '(name repl)
       (lambda (rc)
         (let* [(session (:cookies-ref rc 'repl "sid"))
                (line    (uri-decode (:from-post rc 'get "line")))]
           (repl session line)
           (editor session))))

... the session value is 'repl instead of the desired value. The code 
with the other improvements works though:

     (post "/" #:from-post #t
       (lambda (rc)
         (let* [(session (cookie-ref (rc-cookie rc) "sid"))
                (line    (uri-decode (:from-post rc 'get "line")))]
           (repl session line)
           (editor session))))

[1] https://gist.github.com/wedesoft/b571cc15e81cfcf98e513974085683ff

On Sat, 25 Jun 2016, Nala Ginrut wrote:

> Hi Jan!
> First, thanks for sharing this code.
> I've tried, it's sweet that I love it!
> And I'm looking forward to someone could finish it as a complete
> project just like IPython notebook or similar.
>
> On Fri, 2016-06-24 at 12:43 +0100, Jan Wedekind wrote:
>>
>      (post "/"
>>        (lambda (rc)
>>          (let* [(session (cookie-ref  (rc-cookie rc) "sid"))
>>                 (post-data (map dot (generate-kv-from-post-qstr (rc-
>> body rc))))
>>                 (line      (uri-decode (assoc-ref post-data
>> "line")))]
>>            (repl session line)
>>            (editor session))))
>
>
> It is embarrassing when I'm trying to point out the fancy "shortcut"
> for saving code, but finally I realized there're bugs. ;-(
> Anyway, they're just syntax sugar for convenience, it won't be harm if
> you dismiss it. I'm working on the server core of Artanis, so I may fix
> it next release.
> But I still put my code here to shed some light. ;-P
>
> =========================code=================================
> (post "/" #:from-post #t #:cookie '(name repl)                        
>   (lambda (rc)                                                        
>     (let [(session (:cookies-ref rc 'repl "sid"))                      
>           (line (uri-decode (:from-post rc 'get "line")))]            
>       (repl session line)                                              
>       (editor session))))
> =========================end==================================
>
>
> Best regards.
>
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Artanis web REPL
  2016-06-24 17:20 ` Nala Ginrut
  2016-06-24 21:40   ` Jan Wedekind
@ 2016-06-24 21:51   ` Jan Wedekind
  2016-06-25  1:14     ` Nala Ginrut
  1 sibling, 1 reply; 6+ messages in thread
From: Jan Wedekind @ 2016-06-24 21:51 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-user

Actually I had to revert the ":from-post" code as well, because it was 
always giving the same result for "line" after the first post event.

     (post "/"
       (lambda (rc)
         (let* [(session   (cookie-ref (rc-cookie rc) "sid"))
                (post-data (map dot (generate-kv-from-post-qstr (rc-body rc))))
                (line      (uri-decode (assoc-ref post-data "line")))]
           (repl session line)
           (editor session))))

On Sat, 25 Jun 2016, Nala Ginrut wrote:

> Hi Jan!
> First, thanks for sharing this code.
> I've tried, it's sweet that I love it!
> And I'm looking forward to someone could finish it as a complete
> project just like IPython notebook or similar.
>
> On Fri, 2016-06-24 at 12:43 +0100, Jan Wedekind wrote:
>>
>      (post "/"
>>        (lambda (rc)
>>          (let* [(session (cookie-ref  (rc-cookie rc) "sid"))
>>                 (post-data (map dot (generate-kv-from-post-qstr (rc-
>> body rc))))
>>                 (line      (uri-decode (assoc-ref post-data
>> "line")))]
>>            (repl session line)
>>            (editor session))))
>
>
> It is embarrassing when I'm trying to point out the fancy "shortcut"
> for saving code, but finally I realized there're bugs. ;-(
> Anyway, they're just syntax sugar for convenience, it won't be harm if
> you dismiss it. I'm working on the server core of Artanis, so I may fix
> it next release.
> But I still put my code here to shed some light. ;-P
>
> =========================code=================================
> (post "/" #:from-post #t #:cookie '(name repl)                        
>   (lambda (rc)                                                        
>     (let [(session (:cookies-ref rc 'repl "sid"))                      
>           (line (uri-decode (:from-post rc 'get "line")))]            
>       (repl session line)                                              
>       (editor session))))
> =========================end==================================
>
>
> Best regards.
>
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Artanis web REPL
  2016-06-24 21:51   ` Jan Wedekind
@ 2016-06-25  1:14     ` Nala Ginrut
  2016-06-25 20:09       ` Jan Wedekind
  0 siblings, 1 reply; 6+ messages in thread
From: Nala Ginrut @ 2016-06-25  1:14 UTC (permalink / raw)
  To: Jan Wedekind; +Cc: guile-user

Yes, that's what I said emberrass bugs. I will fix it next release. Please
use the old method for a while.

Best regard.

Jan Wedekind <jan@wedesoft.de>于2016年6月25日周六 05:51写道:

> Actually I had to revert the ":from-post" code as well, because it was
> always giving the same result for "line" after the first post event.
>
>      (post "/"
>        (lambda (rc)
>          (let* [(session   (cookie-ref (rc-cookie rc) "sid"))
>                 (post-data (map dot (generate-kv-from-post-qstr (rc-body
> rc))))
>                 (line      (uri-decode (assoc-ref post-data "line")))]
>            (repl session line)
>            (editor session))))
>
> On Sat, 25 Jun 2016, Nala Ginrut wrote:
>
> > Hi Jan!
> > First, thanks for sharing this code.
> > I've tried, it's sweet that I love it!
> > And I'm looking forward to someone could finish it as a complete
> > project just like IPython notebook or similar.
> >
> > On Fri, 2016-06-24 at 12:43 +0100, Jan Wedekind wrote:
> >>
> >      (post "/"
> >>        (lambda (rc)
> >>          (let* [(session (cookie-ref  (rc-cookie rc) "sid"))
> >>                 (post-data (map dot (generate-kv-from-post-qstr (rc-
> >> body rc))))
> >>                 (line      (uri-decode (assoc-ref post-data
> >> "line")))]
> >>            (repl session line)
> >>            (editor session))))
> >
> >
> > It is embarrassing when I'm trying to point out the fancy "shortcut"
> > for saving code, but finally I realized there're bugs. ;-(
> > Anyway, they're just syntax sugar for convenience, it won't be harm if
> > you dismiss it. I'm working on the server core of Artanis, so I may fix
> > it next release.
> > But I still put my code here to shed some light. ;-P
> >
> > =========================code=================================
> > (post "/" #:from-post #t #:cookie '(name repl)
> >   (lambda (rc)
> >     (let [(session (:cookies-ref rc 'repl "sid"))
> >           (line (uri-decode (:from-post rc 'get "line")))]
> >       (repl session line)
> >       (editor session))))
> > =========================end==================================
> >
> >
> > Best regards.
> >
> >


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Artanis web REPL
  2016-06-25  1:14     ` Nala Ginrut
@ 2016-06-25 20:09       ` Jan Wedekind
  0 siblings, 0 replies; 6+ messages in thread
From: Jan Wedekind @ 2016-06-25 20:09 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-user

Ah, sorry. I didn't understand earlier.
No problem, I'll wait with the changes then.

On Sat, 25 Jun 2016, Nala Ginrut wrote:

> Yes, that's what I said emberrass bugs. I will fix it next release. Please
> use the old method for a while.
>
> Best regard.
>
> Jan Wedekind <jan@wedesoft.de>于2016年6月25日周六 05:51写道:
>
>> Actually I had to revert the ":from-post" code as well, because it was
>> always giving the same result for "line" after the first post event.
>>
>>      (post "/"
>>        (lambda (rc)
>>          (let* [(session   (cookie-ref (rc-cookie rc) "sid"))
>>                 (post-data (map dot (generate-kv-from-post-qstr (rc-body
>> rc))))
>>                 (line      (uri-decode (assoc-ref post-data "line")))]
>>            (repl session line)
>>            (editor session))))
>>
>> On Sat, 25 Jun 2016, Nala Ginrut wrote:
>>
>>> Hi Jan!
>>> First, thanks for sharing this code.
>>> I've tried, it's sweet that I love it!
>>> And I'm looking forward to someone could finish it as a complete
>>> project just like IPython notebook or similar.
>>>
>>> On Fri, 2016-06-24 at 12:43 +0100, Jan Wedekind wrote:
>>>>
>>>      (post "/"
>>>>        (lambda (rc)
>>>>          (let* [(session (cookie-ref  (rc-cookie rc) "sid"))
>>>>                 (post-data (map dot (generate-kv-from-post-qstr (rc-
>>>> body rc))))
>>>>                 (line      (uri-decode (assoc-ref post-data
>>>> "line")))]
>>>>            (repl session line)
>>>>            (editor session))))
>>>
>>>
>>> It is embarrassing when I'm trying to point out the fancy "shortcut"
>>> for saving code, but finally I realized there're bugs. ;-(
>>> Anyway, they're just syntax sugar for convenience, it won't be harm if
>>> you dismiss it. I'm working on the server core of Artanis, so I may fix
>>> it next release.
>>> But I still put my code here to shed some light. ;-P
>>>
>>> =========================code=================================
>>> (post "/" #:from-post #t #:cookie '(name repl)
>>>   (lambda (rc)
>>>     (let [(session (:cookies-ref rc 'repl "sid"))
>>>           (line (uri-decode (:from-post rc 'get "line")))]
>>>       (repl session line)
>>>       (editor session))))
>>> =========================end==================================
>>>
>>>
>>> Best regards.
>>>
>>>
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-06-25 20:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-24 11:43 Artanis web REPL Jan Wedekind
2016-06-24 17:20 ` Nala Ginrut
2016-06-24 21:40   ` Jan Wedekind
2016-06-24 21:51   ` Jan Wedekind
2016-06-25  1:14     ` Nala Ginrut
2016-06-25 20:09       ` Jan Wedekind

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).