Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Convert TK table to equivalent HTML

by jbc66 (Initiate)
on Jan 06, 2024 at 01:19 UTC ( [id://11156740]=perlquestion: print w/replies, xml ) Need Help??

jbc66 has asked for the wisdom of the Perl Monks concerning the following question:

I have a working perl program that builds two perl/TK tables with each cell a button. I want to convert the program to a client side HTML webpage with HTML <table> and javascript. I plan to generate the HTML file with perl because I am moderately familiar with it.

My first problem is how to easily generate the HTML equivalent of the TK table with computed callback button attributes. I have looked at HTML::Table, Template Toolkit, HTML::Template, javascript querySelectorAll, and some other online suggestions. On the surface they look promising but very hard to understand at the detail implementation level, i.e., it would take days to figure out how to use them.

Before I waste a lot of time and effort doing it the hard way, what approach would you take?

Replies are listed 'Best First'.
Re: Convert TK table to equivalent HTML
by marto (Cardinal) on Jan 11, 2024 at 11:02 UTC

    At $work I use a Mojolicious back end, with an HTML5/JavaScript front end, I use DataTables, dynamically building via JSON response from Mojo. The front end JavaScript and DataTables dynamically adds buttons etc for user interaction, firing requests to do various things to the back end.

Re: Convert TK table to equivalent HTML
by cavac (Parson) on Jan 11, 2024 at 10:40 UTC

    Modern HTML+CSS+JavaScript can get very complex, no matter what backend you use. You will also need a somewhat decent understanding of HTTP. And TLS (OpenSSL), if you want to use the stuff over the internet. If you need constant communication between UI and the backend, you will most likely ending up using WebSockets, too.

    I'm not saying this to discourage you. But it wont take days to figure it all out, it will be (at least) months, possibly years, especially if you want a decent, fast solution that isn't a total drag to use and expand it.

    For example, i prefer to have some javascripts within the HTML template i edit for convenience, but modern web security policies want JS as separate files. So my own templating stuff (using Template::Toolkit in the background) has to do some fancy auto-uninlining on application startup, see TemplateEngine/Main.pm in my PageCamel framework.

    Similarly, i don't want to write a new module for every database table i visualize/edit in my web application (especially all the master data tables), i just want to add some XML configuration like this:

    <module> <modname>pos_dispensers</modname> <pm>ListAndEdit::Main</pm> <options> <db>maindb</db> <session>sessionsettings</session> <pagetitle>POS Dispensers</pagetitle> <webpath>/posadmin/dispensers</webpath> <table>pos.dispensers</table> <primarykey> <item column="dispenser_id"/> </primarykey> <candelete>1</candelete> <cancreate>1</cancreate> <cancopy>1</cancopy> <useserial>1</useserial> <cansaveandclose>1</cansaveandclose> <useprevnext>1</useprevnext> <orderby>description</orderby> <generateauditlog>1</generateauditlog> <quickselect>1</quickselect> <list> <item header="ID" column="dispenser_id" type="text"/> <item header="Description" column="description" type=" +text"/> <item header="Protocol" column="protocol" type="text"/ +> <item header="Endpoint" column="endpoint" type="text"/ +> </list> <edit> <item header="ID" column="dispenser_id" type="display" +/> <item header="Description" column="description" type=" +text"/> <item header="Disabled" column="is_locked" type="check +box" delete="1"/> <item header="Network-Mode" column="is_network" type=" +checkbox"/> <item header="Protocol" column="protocol" type="enum" +enumtable="pos.enum_dispenser_protocol" enumcolumn="enumvalue" search +able="1" nullable="0"/> <item header="Endpoint" column="endpoint" type="text"/ +> <item header="Pull mode" column="is_pullmode" type="ch +eckbox"/> <item header="Pull to table" column="pullmode_table" t +ype="text"/> <item type="multiarray" headers="Article|PLU" spares=" +5"> <item column="article_id[]" type="enum" enumtable= +"pos.articles" enumcolumn="article_id" showdescription="article_name" + searchable="1" nullable="1"/> <item column="plu[]" type="text"/> </item> </edit> </options> </module>

    This loads the "ListAndEdit" module that, (in this case) generates a searchable, endless scrolling list of all table rows in "pos.dispensers". You can either select a row to edit (or delete) it, or you can create a new row. The downside of this flexible design is that you have to write a lot of code. Quite a lot, actually.

    • The main logic is a messy beast, indead.
    • Of course, you also need a bit of HTML/Template/JS code for making the listview work. This is just the sub-template for this stuff. (The main template and its JavaScript and CSS that is used as a basis for all rendered pages and the megabytes of JavaScript libraries is another matter entirely).
    • A lot of the real formatting work is then done in edit.tt template that dynamically renders the input form, according to the rules in the XML file.

    Yes, your application will start out much simpler. But you will probably need to (at least) add user managment, login forms, logging, maybe a password reset process, etc. If the software is for a wider audience, you'll probably also need Theme-support (for people with visual impairments) and a way for users to change the font. For example, there are special fonts that help people with dyslexia.(*)

    This isn't to say you shouldn't go ahead with your project. You just need to be aware that writing a (public) web-based software pretty much from scratch can be a very big job. You suddenly turn from programming a simple desktop tool to being a backend engineer, a database administrator, an UI designer, a web security expert and an expert in privacy laws. You will also need to learn JavaScript, CSS and HTML. This all will probably take you a bit longer than "a few days", though.

    Best bet for your project would be to use an existing web framework, then cleanly re-implement your UI using that frameworks tools and examples. Rolling your own web thing from scratch is certainly possible (that's why i'm using "PageCamel" for all my stuff), but be advised that i'm working on that stuff since about 2008 in it's current form, not counting its predecessors.


    (*) According to some studies i've read, the fonts don't help and can, in some cases, make the problems worse. But if you don't have the option, especially for in-house projects, some users will complain that your software is unusable for them. Of course, after you implement the option, pretty much nobody will use it, but you GOT to have have the option to stop complaints and satisfy the PR people...

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re: Convert TK table to equivalent HTML
by Anonymous Monk on Jan 06, 2024 at 02:49 UTC
    You need a JS-GUI library which covers the semantics you need and hope to be able to reuse as much of Perl's call back code via AJAX as possible.

    Unlikely to do this in days.

    Cheers LanX

    (Who has problems posting)

      Thanks for your help. I have pretty much written the perl code to generate an HTML file and written a javascript file to do the calculations. Just need to get the document.getElementById().value calls with computed ID strings to return something other than "undefined".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11156740]
Approved by LanX
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-05-21 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found