Home   Archive   Permalink



Address book CGI

While I agree with Nick, that new technologies have emerged and provide an easier way to code for the web, I am not still wanting to explore using REBOL for web development. I have used ChatGPT and made an address book code with REBOL CGI. There are many errors in this code which I am not able to correct. Please help.
    
  
#!/usr/bin/rebol -cs
    
R E B O L []
    
; Define the CSV file path to store contacts
file-path: %address-book.csv
    
; Helper function to parse CGI input
parse-cgi: func [
     "Parses CGI input from environment variables"
     /local input data pairs
][
     input: system/options/cgi/query-string
     data: copy []
     foreach pair parse/all input "&" [
         append data to-block rejoin [
             replace/all replace/all pair "=" " " "%20" "%"
         ]
     ]
     data
]
    
; Helper function to load contacts from the CSV file
load-contacts: func [] [
     either exists? file-path [
         read/lines file-path
     ][
         copy []
     ]
]
    
; Helper function to save contacts to the CSV file
save-contacts: func [contacts] [
     attempt [
         write/append file-path contacts
     ]
]
    
; Helper function to safely get a value from parsed data
get-param: func [data key] [
     either find data key [
         pick data (1 + index? find key)
     ][
         ""
     ]
]
    
; Output HTTP header
print "Content-Type: text/html^/^/"
    
; Attempt to process CGI input and perform actions
     ; Process CGI input
     data: parse-cgi
    
     ; Determine action
     action: select data 'action
    
     contacts: load-contacts
     html-content: ""
    
     ; Handle actions (Add, Update, Delete)
     switch form action [
         "add" [
             new-contact: rejoin [
                 pick data (1 + index? find data 'name) "," pick data (1 + index? find data 'phone) "," pick data (1 + index? find data 'email) newline
             ]
             save-contacts new-contact
             load-contacts
         ]
         "delete" [
             old-contact: rejoin [
                 get-param data 'name "," get-param data 'phone "," get-param data 'email newline
             ]
             contacts: remove find contacts old-contact
             save-contacts contacts
         ]
         "update" [
             old-contact: rejoin [
                 get-param data 'old-name "," get-param data 'old-phone "," get-param data 'old-email newline
             ]
             new-contact: rejoin [
                 get-param data 'name "," get-param data 'phone "," get-param data 'email newline
             ]
             replace contacts old-contact new-contact
             save-contacts contacts
         ]
         none [
             print "<h1>Invalid action provided!</h1>"
             halt
         ]
     ]
    
     ; Generate HTML with Bootstrap
     html-content: rejoin [
         {<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Address Book</title><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"></head><body>}
         {<div class="container mt-5"><h1>Address Book</h1><form method="GET" class="form-inline"><input type="hidden" name="action" value="add"><input type="text" name="name" placeholder="Name" class="form-control mr-2"><input type="text" name="phone" placeholder="Phone" class="form-control mr-2"><input type="text" name="email" placeholder="Email" class="form-control mr-2"><button type="submit" class="btn btn-primary">Add Contact</button></form>}
         {<hr><table class="table"><thead><tr><th>Name</th><th>Phone</th><th>Email</th><th>Actions</th></tr></thead><tbody>}
     ]
    
     foreach line contacts [
         fields: parse line ","
         html-content: rejoin [
             html-content
             {<tr><td>} fields/1 {</td><td>} fields/2 {</td><td>} fields/3
             {</td><td><form method="GET" style="display:inline;"><input type="hidden" name="action" value="delete"><input type="hidden" name="name" value="} fields/1
             {" /><input type="hidden" name="phone" value="} fields/2
             {" /><input type="hidden" name="email" value="} fields/3
             {" /><button type="submit" class="btn btn-danger btn-sm">Delete</button></form></td></tr>}
         ]
     ]
    
     html-content: rejoin [
         html-content
         {</tbody></table></div><script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script></body></html>}
     ]
    
     print html-content



posted by:   neuropsych     31-Aug-2024/12:30:18-7:00



You may need to link to a source code file. This old rebolforum code doesn't handle posting HTML/CSS code well.

posted by:   Nick     1-Sep-2024/16:06:52-7:00



And probably best to start a different thread.

posted by:   Nick     1-Sep-2024/16:07:52-7:00



(and there's virtually nobody around to help any more...)

posted by:   Nick     1-Sep-2024/21:32:44-7:00



In your code, I replaced HTML tags with <, >, and & for the <, >, and & characters respectively, and surrounded the code with pre tags, to prevent the browser from rendering the layout. To post HTML code here, you'll need to do that. It's easier to post links to files on Github (or anywhere else).

posted by:   Nick     2-Sep-2024/9:43:49-7:00



Here's a little app to share and display code examples, which may be helpful:
    
http://server.py-thon.com:8008/code_examples.sql
    
You can post the links here, such as:
    
http://server.py-thon.com:8008/code_examples.sql?edit=1
    
Written in SQLPage in just a few minutes :)

posted by:   Nick     2-Sep-2024/10:26:20-7:00



The application above took a few minutes to build and publish with SQLPage. Here's a similar app created with Anvil:
    
https://codeexamples.anvil.app
    
That took about 3 minutes to build from scratch and publish with Anvil. Doing that with Rebol CGI would take much longer, and you'd have a lot more problems to deal with, and the ability to connect with other systems and deal with common data formats would be severely limited. Are you aware that R2 can't even read most HTTPS:// protocols? That will keep you from being able to interact with most modern web APIs. There are fixes, but that will just be the first in an endless number of workarounds you'll need to implement to accomplish basic, common tasks - all the way to very complex tasks - which would be child's play in Python/Anvil or SQLPage. And you won't be able to work with other developers who use common database systems and other common tools, data structures, etc.
    
I'm just trying to help you make a decision that will save enormous amounts of time and frustration, and enable lots more practical capability. Anvil and SQLPage are both practical modern systems which will enable paths that won't limit you, and which will get you learning skills which are mainstream and applicable to collaborating with other developers who use mainstream tools. And Anvil/Python and SQLPage more productive than Rebol, without reservation, for building the sorts of database applications and web apps you might be able to with Rebol. Anvil is extremely powerful and a great path toward learning the massive Python and JS ecosystems, but it's a big hosted tool that requires a lot of learning to implement in its open source version (totally doable and documented, but it's a journey if you're new to all this). SQLPage is super simple, focused on building with SQL only, but the UI is infinitely extensible if/when you want to learn any sort of front end web development skills, and can be extended by learning any sort of back end skills with any back end language (you could even connect it with Rebol back end code - and that's true of Anvil too).
    
Just a friendly perspective from someone who used and loved Rebol for many years, for all the reasons you're likely attracted to it :)

posted by:   Nick     2-Sep-2024/10:55:31-7:00



And, BTW, you won't need to ask for help from people, the way you do with Rebol (because GPT does not know Rebol well). You will go 100x faster learning anything else modern, because GPT will have a better understanding and ability to write and explain code in other languages (SQLPage is new, so it's not great with the SQLPage UI dialect, but it's brilliant with SQL database code generation).

posted by:   Nick     2-Sep-2024/11:03:32-7:00



Choosing to learn Rebol now is like choosing to learn Fortran when Rebol came out... except that Fortran had (and still has) a community, still has industry support where needed, etc. You might as well learn BASIC for Commodore 64 - sure you can still do fun things with that system, but you could instead spend that time actually building deeply useful skills. I absolutely love Rebol's ethos, ergonomics, and style, but the ecosystem died a quick death after 2012 and it's now limited in too many ways to recommend using it. If you're just a hobbyist interested in the fun of playing with a really cool tool, then enjoy Rebol, it's neat. If you actually plan on building applications that can connect with world in 2024 and ahead, you'll be disappointed by Rebol's current limitations.

posted by:   Nick     2-Sep-2024/22:23:46-7:00



I used ChatGPT to update your script. The updated code is here:
    
http://server.py-thon.com:8008/code_examples.sql?edit=3
    
Here's what it looks like:
    
https://com-pute.com/nick/rebol_forum_example.jpg
    
A package with an old tiny version of Apache for Windows, with Rebol all set up, and this script ready to run, is at:
    
https://com-pute.com/nick/miniserver-with-rebol-cgi-setup-in-public-html.zip
    
Just run server_start.bat to start it and server_stop.bat to stop it. If you want to run it on Linux, use:
    
https://com-pute.com/nick/rebol278
    
I let GPT work out everything in the updated code, without much guidance except feeding it the errors Rebol produced and explaining the problems in plain English, in a way you could also do. GPT is terrible with Rebol, so I needed to provide a working CGI example for it to learn from (I used the source code of this forum as an example). Here's the full session:
    
https://chatgpt.com/share/b2a11971-11d5-4ac3-abc7-9fe7d12738dd
    
I did this to help you learn, if you're still interested, but you'll have a *much better time in the end with Anvil, or SQLPage, or give FastHTML a try, or if you want to learn all the basics of traditional mainstream web development methodology, try the Brython tutorial at https://com-pute.com/brython_tutorial and the GPT case study at https://com-pute.com/nick/brython-tutorial-and-GPT-case-study.txt (replace Brython with jQuery and you're smack dab in the center of traditional web development tooling...)

posted by:   Nick     7-Sep-2024/14:29:25-7:00



To give you some comparison, Claude currently knows nothing about fastHTML, but fastHTML is built on more commonly used tooling, and at https://docs.fastht.ml they provide this file for LLMs to use:
    
https://docs.fastht.ml/llms-ctx.txt
    
I popped that into Claude and asked it to convert the Rebol code above into a fastHTML app, and it took a few seconds to get a usable application. I spent a few minutes asking Claude to add some more features such as the ability for users to edit contacts, and to adjust the styling:
    
http://server.py-thon.com:5002/
    
Here's the code:
    
https://com-pute.com/nick/fasthtml_contacts.py
    
Here's the full prompt session:
    
https://com-pute.com/nick/claude_fasthtml_contacts_app_from_rebol_prompts.txt
    
You can't overestimate how important it is to use mainstream tooling. If you're just starting out, you just don't have any perspective about how important the support of a large ecosystem is. Not just with LLM support, but in being able to use your language to do *anything.

posted by:   Nick     7-Sep-2024/17:25:25-7:00



That session with Rebol was torturous. The session with FastHTML was painless. Make your own decisions :)

posted by:   Nick     7-Sep-2024/17:26:47-7:00



Use Rebol to read your updated forum example code:
    
editor decompress #{
789CDD58516FDB36107ED7AFB87218DA0291D524EBD039B6D32ECBBA01DD5AAC
E98021F0005AA42B2692A891B43DA3E87FDF1D49C9B2EA666B87BD2C4062933C
7E3CDEDD7777CC17F7B29535D942D599910B5D429A5B8024F9E5F2DB972FE07A
4EDF1BA36A07EC42D74ED62EBDDA36720C4EFEE9B2C255E5EFD9EF1923B133F8
4E2E552DC115122E5EFF0A4B554A68B82BC069B04E1B093962F0DCD111B49AD2
EA18BEE44218696DBAD0FA7694DB35815D49EB402D6183BB780D1BA39C242042
F7C8BA2EB724A01C082D6D7DDF81FC53E1A6AD7409CED73A4ED873E80E83EB04
F0873B27ABC6C17580DD2D33364FE6E12E4E9A8AAE63E41F2BD2A592AED00278
2DF052C6E21D9FFF08AA6E562EC9DFAA5470C7C778682D13BB512E2FC06E2D9E
92E9C6295DDB0C85B2889546ACA00C7BF5F2F5158B03FAD9C1E5BAD9A24E68AE
FEDA62B55C4AD3AD766B9B82EC72DD68AB9C5ACB73549D8B54E95693461B6733
AF720F078EBF3E7DF268DE3BDF5BA86924DEB455A52F9F97929BDE44B76F9EEC
FEB2E7971FB9D301B3A051CC36B50E03ED6DC4884EC8B5880185C6F68A602C6A
E0B028757E0B7A09B7729BAE79B9A25053C62676B5A814BA578C41F8ED299ED0
1D4FA83FC8B2C16B2C57754E2A5050959A8B5D702E8DAEF6A3181292485B89B1
DF4CF4B88EAE910AE5CDA178EBF98EDC91951853B6271096E77D39EF57CF3DBF
849FF340B0039A5BBEEED12AF2A3A737AD7FA0773BDEE9BF4F880F94EC36DCA9
C9522223DF4A87EE091EF186F46411C17990E032421A5EB5BAF87974621B804B
CC131CD9737D0BEB79D8B58B22A435328897E7B4036E07316BA45B991AD60703
322E225D7C685DDA9C3764B9AAE216832AE615598AA0BB4DA497488344ABAD5F
6B5535B229792E335E96F1C2EC8801FBF2E4221EF2A6967F7BCCAAFEAC83E810
3A2E1CF48B44EEC875A00A0F2EF156A62C9684893174B6878E24703F2C12C80B
22818F60A461E7F264173C7B1C4812CAFE7E8855611CB214450626C8B255C2C2
8367421CC19B061D298F90D0256AF4102523617C92C6C0DB9EB76A871BC714BA
37E7D30A960AB6472AFAA9E5A6D56B8CC6BAD1AA1E8486E768DFCE878D51A3C5
BC133F67735360F2FFECDDB2E2AA1CDC6B3E18B749B9A57BEFDE006780A6F601
80D3AD4C9B114AF4EA1ED45E5EE8D5E533784D1985226D9855281D24875463C2
BBF543BFE852FC3FFD32483B955E9379FAAEE95D7DB0F963963F6CDA9567CE7F
685A02FA57E625807F676242F87B33FF6F493E08269FEA0F07D21EDF3F31A8BA
6F422EF9AA7403DBC5267F521CCF7EACB1C628D1D511A3D74A48716F92E1221B
20CEDB02B4DF42F125161EEC95B75069A1962AE7B11C84B68F97479455646D57
2614AD12A31C9BECD0E259B085DED40F3F5E7BCEE0B9ACA5C14DF0C3D54F2F00
CB4501DF6AEDB089E48D6FD24352229596CA543CDC4537AB6650B8DA508AC67A
37B9F7DDCB8BABDF5E5D02C9CD26F417D5ABDF4E99AC198EB18B9B4DB0B26253
5C506FE3A6ECCDD5F7E913166729BEA66CADE4861A6E06F1A429DB28E18AA990
6B95CBD40F8EB025C05E9D9729C64E29A7C7A34788E2942BE5EC597814D1AD6E
2759989B6063760B8591CB292B9C6BEC38CB2C9AE496BAB4D1A2BD7F2EEA1186
61D64D645F8D1E8F4EB2DCDADDDC085F37F8D8B20C2D504E9975DB52DA424AC7
66EFA3256C6E54E3DA61D7EC45938662FEE021BC6B9BABB8F0E0FE33F4EA56AF
C0BB97BE6C78ED6B51748A2B5417A5E7F71F9EC1FBF6C86CFF4C8C396FEE8516
DBD9FBCE4542ADF119C2AD9D328F82EDAA81CAA58FC93FC703D3E1C404BBCA2A
BEE0A6E16912B7D342AA6A6A78716F781B397CE1A27D9510E8F0E8CE400616DA
AFA96F43F6C5E941DC0AD35F069EC805F2579A29FBD94FF50F25C50DBEB82B93
9EDC81E513CE00EC5598FB0C349F8006689761EE0EB4C5CA396AF23D5C48679D
FCC2D580BF2926908A9B2D23E3C34570ED240B3B67938C607B0E2C0C86395F60
A31861FC80623FF8DBD17A3123AB61EC177EE06FDD8DBCD6DDE8596837C338A3
DD598BD4858E7FBAB4CF0B72F82E5DB5E4F79D39661BFF66F1EDB697A3740F7B
AF93D0C20771B8F69F6318B4F261794E3DD5818740D8EB41EFCC474381DEF4BB
602681B70B68D93131C689FDC9934393A77B38EDF2019EF8BC306542590C9AED
3850E58C015ADB07C294ED93BFCD0A679F42A7D840DEB523702ACAF72EDCBB06
83EC2E84C8A421C4C9274044FA0C214E3F80F8278C115853306DD1575BB15930
DC9032C13514D12D79BAFF07DC1936074286DCECC9809FC436FCC43C3A8B591E
ACC9774585FE7132BAF1FF97F195247C4D4FB18C1C8F6CA92A5F3D6E2CBAACCD
D887710449A17BD5DA8C6AE9B2BAA9B2A758861B696E2C1E63E4D393D137A353
54C5BA6C55892C2CFE43FC03C5EF60E5BB1916BE7DE82CDAC557FC2E57849E68
60C9E42F5700A26A33150000
}

posted by:   Nick     11-Sep-2024/18:57:12-7:00



Name:


Message:


Type the reverse of this captcha text: "t a h w"



Home