6 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

4 comments

  1. [3]
    pocketry
    Link
    I'm trying to create a github.io page and would like some initial guidance. I know I could fumble through and eventually get something working, but I really would like some direction from someone...

    I'm trying to create a github.io page and would like some initial guidance. I know I could fumble through and eventually get something working, but I really would like some direction from someone that knows a little more about front end libraries than me.

    I have a json (https://github.com/dialogueovertime/dialogueovertime.github.io/blob/master/episodes.json) that I would like to display as a table on page (dialogueovertime.github.io). I'm using github pages so I don't have to worry about deployments and security at all.

    I'm familiar with using html/css to build some very basic pages, but want to know how to get started with some good js libraries for taking data from the json into a table. I'm a beginner at js, but am decent at python, so programming concepts at this level don't scare me.

    I'm also looking for a nice way to keep the styling consistent across the page and any future pages I may build out around this. Would tailwind css be good for this?

    As for general page layout, I've played with flex and grid a few years ago and liked it enough to use them again if it makes sense.

    Any general input or direction would be much appreciated. Thanks!

    2 votes
    1. Bauke
      (edited )
      Link Parent
      If you're willing to write everything from scratch you don't really need any external libraries to accomplish what you're trying to do, so I'll just give some general pointers and links to native...

      If you're willing to write everything from scratch you don't really need any external libraries to accomplish what you're trying to do, so I'll just give some general pointers and links to native JS functionality that I would use if I was making this.

      To start you'd need to get the JSON data somehow, the likely option here would be fetch(), where await (await fetch('episodes.json')).json() would get you your episodes array already parsed into JS (note that this is async).

      Then to create the table on the page Element.insertAdjacentHTML() is what you'd want. For example to insert a div as the last element of the body, document.body.insertAdjacentHTML('beforeend', '<div></div>'); would do the trick. And you can put anything in there with template literals or good old concatenating strings like 'a' + 'b'.

      Since insertAdjacentHTML is defined on any Element, you'd want to use document.querySelector to grab either a pre-made table element or the parent (or sibling) element you want to insert the table next to.

      If you do however want to use external libraries like from npm, I'd recommend using Vite to build your website. I don't have the required vocabulary to explain how much of a clusterfuck importing code is in JS and in my experience Vite is pretty easy to use and makes it a lot less painful.

      4 votes
    2. Apos
      Link Parent
      If you want to do a quick static site, you could use eleventy. You can have the JSON be imported as global data and do a nunjucks page that loops over it. nunjucks Here is a good tutorial to get...

      If you want to do a quick static site, you could use eleventy. You can have the JSON be imported as global data and do a nunjucks page that loops over it. nunjucks

      Here is a good tutorial to get started: Making a Simple Web Site with the Simplest Static Site Generator, Level 1.

      A bit closer to what you want to do: Generate Page Content From a Global Data File Using Eleventy. There's a part where you can see how you can put the JSON data right inside the HTML.

      1 vote
  2. archevel
    Link
    Started to learn Rust over the holidays and I'm planning to use it for a code gen project to quickly create a front-end for a webapp based on a spec. So starting out I want to make a parser for...

    Started to learn Rust over the holidays and I'm planning to use it for a code gen project to quickly create a front-end for a webapp based on a spec. So starting out I want to make a parser for the spec and found nom. To start out I want to make a function that accepts ASCII character inputs where the first character is capitalized; i.e Abc and Xyz is fine, but abc and xyz should be rejected.

    This seems to do the trick, but I'm curious if there is a more elegant approach:

    nom::combinators::verify(alpha1, |s| match s.chars().next() { Ok(c) => 'A' <= c && c <= 'Z', _ => false })
    

    Note that alpha1 guarantees that s is at least 1 characters long and consist of ASCII encoded letters. Knowing this I know it would be safe to index directly into the &str with something like s[0], but the compiler doesn't allow it as far as I understand. Anyway, is there a way to write such a parser function in a better way?

    1 vote