Connection refused when using getCSV for files outside of `data`

Hi,

I’m setting up a Hugo site with with the timer-hugo theme (I hope my problem is not related to the theme). I have a csv file stored in the static directory (not in data, because I want to access a csv from a user-defined path set by .Params.tlarData in the content markdowns)

My code finds the currect URL:

{{ if .Params.tlarData }}
          {{ $url := .Params.tlarData | absURL }}
          {{ $data := getCSV ";" $url }}

but getCSV throws:

Failed to get CSV resource " … /tlar.csv": Get " … /tlar.csv": dial tcp 127.0.0.1:1313: connect: connection refused.

Setting ignoreErrors = "error-remote-getcsv" solves the issue for localhost, but not when I’m publishing the website on a real server (GitLab pages in my case).

From getCSV output nothing - #7 by tsq456 I guess there is some authorization problem?

Could someone provide me with more insight into the problem, why there is an authorization required for getCSV and maybe has a fix for this?

Let me now what further information you need from my project to support here.

Thanks in advance!!

getCSV does not work outside the data folder.

There a couple of things you can do.

  1. Mount /static/ as /data/
  2. Wait for the next Hugo release that will introduce fetching an external resource.
  1. The getCSV function retrieves remote data.
  2. You can never place a CSV file in the data directory.

@MarAlder

To get a map of a local CSV file, use transform.Unmarshal.

If you place the CSV file within a page bundle:

{{ with .Resources.GetMatch "table.csv" }}
  {{ $options := dict "delimiter" ";" }}
  {{ $data := transform.Unmarshal $options . }}
{{ end }}

If you place the CSV file within the assets directory (or any directory mapped to assets):

{{ with resources.Get "table.csv" }}
  {{ $options := dict "delimiter" ";" }}
  {{ $data := transform.Unmarshal $options . }}
{{ end }}

Or you can read it from anywhere within the project directory (but never place the CSV file in the data directory):

{{ $path := "foo/table.csv" }}
{{ if os.FileExists $path }}
  {{ $options := dict "delimiter" ";" }}
  {{ $data := os.ReadFile $path | transform.Unmarshal $options }}
{{ end }}
2 Likes

WithIn the local file system of a Hugo project I was not able to use geCSV outside the dataDir or the assetDir. Not from static as the OP posted. Unless something has changed in the past year or so.

You cannot use getCSV with local data.

That’s why above I said that it does not work outside the dataDir
I forgot to add the assetDir.

Thanks for completing the answer.

You can’t use getCSV to access data within the data or assets directories either.
You can’t use getCSV to access data anywhere on the local file system.

Prove me wrong, but please test.

Yes. You’re right… I hadn’t used it in sometime.

I use transform.Unmarshal these days for rendering CSV data.

Thank you so much for the super quick responses! I’m looking forward to the next releases, but your tips help me to proceed with my project :slight_smile:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.

@alexandros @MarAlder

Yeah, so I tested again and proved myself wrong. The statements above are utterly false. I apologize.

Best guess: I was testing paths relative to a given directory rather than a path relative to the project root. Moron.

Given:

my-project/
├── assets/
│   └── a.csv
├── data/
│   └── b.csv  <-- Don't put it here!
├── other/
│   └── c.csv
├── static/
│   └── d.csv
└── e.csv

All of these work. The leading slash is optional; the path is always relative to the root of the project directory.

{{ getCSV ";" "/assets/a.csv" }}
{{ getCSV ";" "/other/c.csv" }}
{{ getCSV ";" "/static/d.csv" }}
{{ getCSV ";" "/e.csv" }}

Although getCSV works with a file in the data directory, don’t put it there. It will cause an error as soon as you try access a supported data file (JSON, TOML, YAML) with site.Data.foo.

Having said that, for many cases I think it’s easier to include the CSV file in a page bundle, and get it with .Resources.GetMatch as described earlier.

Hm… I thought I was growing slightly senile before my time… But anyway I’m glad that is not the case after all…

So thanks for proving yourself wrong! LOL :four_leaf_clover:

(currently I don’t have much time to prove things around here)