Mentions légales du service

Skip to content
Snippets Groups Projects

Draft: Add AI stack

Closed Fernando Ayats requested to merge aistack into main

Provides a landing page that shows the status of the packaging for different ML libraries.

Broken, because Hugo can't render a shortcode within a shortcode. I don't know how to solve this.

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • added 1 commit

    • 10232fc4 - Using a partial instead of a short code

    Compare with previous version

  • Quick and dirty solution is to put everything in one huge shortcode. A better option is to define the 2nd-level entity ("badge") as a partial instead of a short code. Then you can call a partial from a shortcode. See the new commit as a starting point.

  • Actually I think the opposite would be even better, i.e. having a partial to generate the table, and a shortcode to generate the logo. Below is a quick example built by ChatGPT.


    Certainly! Let's break down the solution step by step to create a Hugo partial that generates a table from a JSON file, where one of the columns is generated by calling a shortcode.

    We'll assume you're using JSON data stored in data/books.json, and you want to generate a table from this data. One of the columns will be generated by calling a shortcode for rendering genre icons.

    Example Structure

    Let's assume your JSON file looks like this:

    data/books.json

    [
      {
        "title": "The Great Adventure",
        "author": "John Doe",
        "year": 2020,
        "genre": "Adventure"
      },
      {
        "title": "The Dystopian World",
        "author": "Jane Smith",
        "year": 2018,
        "genre": "Dystopian"
      },
      {
        "title": "The Fictional Tale",
        "author": "Mark Black",
        "year": 2015,
        "genre": "Fiction"
      }
    ]

    Step 1: Define the Shortcode for the Genre Icon

    First, let's define the shortcode that will generate an icon based on the genre. We'll create a genre-icon.html shortcode.

    /layouts/shortcodes/genre-icon.html

    {{- $genre := .Get "genre" -}}
    {{- $icon := "" -}}
    
    {{- if eq $genre "Fiction" }}
      {{- $icon = "<i class='fas fa-book'></i>" }}
    {{- else if eq $genre "Adventure" }}
      {{- $icon = "<i class='fas fa-compass'></i>" }}
    {{- else if eq $genre "Dystopian" }}
      {{- $icon = "<i class='fas fa-eye'></i>" }}
    {{- else }}
      {{- $icon = "<i class='fas fa-question'></i>" }} <!-- Default icon -->
    {{- end }}
    
    {{ $icon | safeHTML }}

    This shortcode takes a genre as input and returns an appropriate icon for it.

    Step 2: Create the Partial to Generate the Table

    Next, we create a partial that generates the table. This partial will read data from the data/books.json file and use the genre-icon shortcode to render the genre column.

    /layouts/partials/table-of-books.html

    <table>
      <thead>
        <tr>
          <th>Title</th>
          <th>Author</th>
          <th>Year</th>
          <th>Genre</th>
        </tr>
      </thead>
      <tbody>
        {{ range .Site.Data.books }}
          <tr>
            <td>{{ .title }}</td>
            <td>{{ .author }}</td>
            <td>{{ .year }}</td>
            <td>
              {{< genre-icon genre=.genre >}} <!-- Calling the shortcode here -->
            </td>
          </tr>
        {{ end }}
      </tbody>
    </table>
    • In this partial, we are using the range function to loop through each item in the books.json data.
    • For each book, we use {{ .title }}, {{ .author }}, {{ .year }}, and {{ .genre }} to fill out the table.
    • We then call the {{< genre-icon genre=.genre >}} shortcode inside the <td> for the genre column. This dynamically renders the appropriate icon for each genre.

    Step 3: Use the Partial in a Template

    Now that we have the table-of-books.html partial, we need to call it from a template or content file.

    Example: Calling the Partial in a Page (e.g., content/books.md)

    In your content/books.md file (or any other content page), you can include the partial like this:

    ---
    title: "Books"
    ---
    
    Here is a list of books:
    
    {{ partial "table-of-books.html" . }}

    This will render the table generated by the partial on the page, with each genre being represented by its corresponding icon.

    Step 4: Ensure FontAwesome is Loaded for Icons

    Since we're using FontAwesome icons for the genres, you should make sure that FontAwesome is included in the <head> of your layout file.

    /layouts/partials/head.html (or baseof.html)

    <head>
      <!-- Other head elements -->
    
      <!-- FontAwesome -->
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    </head>

    Step 5: Run Hugo to Generate the Site

    Once everything is set up, you can run the Hugo development server:

    hugo server

    Then, visit http://localhost:1313/books/ (or whatever page you linked the partial to), and you should see the table rendered with the genre icons for each book.


    Recap of the Solution:

    1. JSON Data: The books.json file contains book information.
    2. Shortcode: The genre-icon shortcode renders an icon for the genre.
    3. Partial: The table-of-books.html partial loops through the JSON data and calls the genre-icon shortcode for each book’s genre.
    4. Template: The partial is included in a page (e.g., books.md), which generates the table with the dynamic content.

    This solution combines shortcodes, partials, and JSON data in Hugo to create a dynamic, reusable component for displaying books in a table format.

  • Actually I think the opposite would be even better, i.e. having a partial to generate the table, and a shortcode to generate the logo. Below is a quick example built by ChatGPT.

    Partials can't be used in Markdown, AFAIK.

  • Right. Got fooled by ChatGPT :face_palm: So the two approaches I mentionned yesterday are the way to go.

  • added 1 commit

    Compare with previous version

  • added 1 commit

    Compare with previous version

  • I've rewritten the UI with JQuery in the end.

  • Fernando Ayats marked this merge request as ready

    marked this merge request as ready

  • Oops, getting 429 Too Many requests

  • added 1 commit

    Compare with previous version

  • Turns out that loading so many <img> will result in getting some 429 errors. I've tried throttling down the requests with JS but it's a hard problem to solve.

    I'll leave the MR as PoC for now.

    Edited by Fernando Ayats
  • Fernando Ayats marked this merge request as draft

    marked this merge request as draft

  • Hey ! this Merge Request has been inactive since 3 weeks. Could you please check the status and update as necessary ? Thanks.

  • Hey ! this Merge Request has been inactive since 3 weeks. Could you please check the status and update as necessary ? Thanks.

  • Closing for now

Please register or sign in to reply
Loading