Reference local .csv file in altair chart
To use a local data file in an Altair chart, two conditions need to be met:
- The file must be specified as a URL valid for the frontend (i.e. browser) displaying the chart
- The URL must satisfy the security requirements of the browser (e.g. must satisfy cross-origin policies)
Unfortunately, the approach to ensuring (1) and (2) are met is highly dependent on what frontend you're using (i.e. JupyterLab vs. Jupyter Notebook vs. Colab vs. Streamlit vs. ...) and within each of these can even depend on what version of the notebook/server you're running, what browser you're using, what browser security settings you have enabled, whether you're using http
or https
, whether or not you use an ad-blocker, what operating system you're using, the precise details of how you launched your notebook server or opened your HTML file, and probably many other variables.
For this reason, as the primary author of Altair, I generally discourage people from attempting to use local data files, because it's quite difficult to answer seemingly simple questions like "how do I reference a local CSV file in an Altair chart?".
If you want to press forward, I would suggest opening your browser's javascript console, where you'll see warnings or errors that could help in diagnosing how you can change things to meet requirements (1) and (2) above within your own setup. If you want something that will just work, I would suggest using pandas to load the file into a dataframe, and create the chart that way.