Use Libreoffice calc to make graph from some json data #49
Labels
No Label
bug
docker
documentation
fonctionnalité
prêt
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: champs-libres/biommap#49
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The general goal of the issue #44 is to better exploit the data that is collected by users on biommap. These data are some observations (aka "sites") of natural elements: trees, hedges, ponds, etc.
The biommap app is based on a backend with Python-Flask + frontend with angular-typescript. So the backend serves as an API and provides the data as json file to the frontend. We benefit from this to use the API to do stuff. For instance, we can open these json data in QGIS. This is explained here: https://gitea.champs-libres.be/champs-libres/biommap/wiki/Utilisation-API
An idea would be to use Libreoffice Calc to somehow fetch the json data from the API and make some graphs. For example, from https://biomap.champs-libres.be/api/sites/programs/1, we could make a pie chart of the trees species distribution. A perfect solution would be to store the json address in a Libreoffice calc file and to have always the latest data when opening the file.
eg:
I suggest to make some search on the web, use chatgpt. Maybe there is a simple solution, maybe there is no good solutions that would take less than 2 days of work, in the latter case, I would not spend more than 2-3 days on it.
It's true that chatGPT is kind of awesome sometimes. So he told me to do use the function WEBSERVICE("URL") and this works.
The json data is retrieved.
However I imagine that we want to parse it now to have a bit of oversight, because he puts all the data in one cell.
So to parse the JSON Data you can use LibreOffice Calc's JSON functions to extract the data you need with
=FILTERJSON(A1, "name")
This formula will create a list of names from the JSON data. You can adjust the "name" key to match the key in your JSON data.
If you click on the cell containing all the data and press 'enter' it will refetch the latest data.
Updating data automatically can also be done with a 'macro':
If you want to automatically update the data at regular intervals, you can create a macro to refresh the data.
Click the "New" button to create a new module and give it a name "updateDate"
To automatize the updating of data on a regular basis we have to create a cronjob on safran I imagine and a python script that will be executed every so often.
Create a Python Script:
Open a text editor and create a Python script (e.g., run_macro.py) to run LibreOffice Calc with your macro. Here's an example script:
python script should be something like this:
Make the Python Script Executable:
Open your terminal and navigate to the directory where the Python script is located. Make the script executable by running:
bash
chmod +x run_macro.py
Edit Your User's Cron Table:
Open your user's crontab by running:
bash
crontab -e
Schedule the Python Script:
Add a line to your crontab to schedule the Python script. For example, to run it every day at 2:00 AM, add the following line:
bash
NB: webservice don't work with my version of libreoffice (6.4). Version of Julie's libreoffice is 7.3.7
Ok, so in the end this is quite a bit more complex than just running two functions in Calc.
In fact I have the WEBSERVICE funtion, but not the FILTERJSON function. So it is not possible to simply do everything within Calc directly.
Consequently I found that you could write a macro, but for me the only language my Calc is willing to work with is 'Basic', which I don't really know and I feel like it was better to use python.
The result is that we'd have to write a python script that fetches the data and then parses it.
I made an attempt at that, but it's not super easy. Then we could write a small script in Basic that will run the Python script if we want so it could be a macro within the excel...
Like I said, it's all a bit more complicated and I don't have much knowledge of Basic to write directly in that language. I could try. Probably chatgpt could help...
Alright, I cannot get it right. Sorry...
Tried with python, tried with Basic. I can give you what I have so far in terms of Macro and python script, but Calc keeps on giving errors. The python script is external to the Calc spreadsheet, but BASIC should be able to execute it. Anyways, I keep getting errors.
Another idea was to use the WEBSERVICE() function to get the data, which is then put in one cell and then in BASIC to parse the data of that one cell. However my Calc keeps crashing now when I insert the api url
https://biomap.champs-libres.be/api/sites/programs/1
I tried something else to get the data for each site separately, but it's difficult because the site_id's are not at all consecutive. It jumps from 1 to 100000 or something and then there's other ones that are like 275 or something... So I would have to get all the site_id's and then dynamically form each api url for each site.... However even getting the site_id's from this url https://biomap.champs-libres.be/api/sites/programs/1 makes my Calc crash.
Bref, it's been a bit of a mess and unfortunately I don't have anything working.
I still feel like getting BASIC to work with the python script would be the best option, but I've run out of ideas on how to do this....
Here are the two scripts though if you want to try
Python script:
BASIC script used in the macro
@LenaertsJ
Hey if it is too much complicated and not very used by libreoffice users, it is probably better to not do it. We still have the export as csv/Excel option which should not be too complicated.
I'm afraid having the need of Python would prevent the use of the macro by any user, since the user would need to install Python to use it, right? So it is definetely better to make a custom API point that export data as CSV or Excel.
Hey Julien, ok, I think we can say it'll be too complicated. Allé, I really liked the challenge of figuring it out, but it will require too much time so better not to.
It's also true that users would be required to have Python installed on their computer which wouldn't be a good option either then.
Ok, so custom Api point to export data as a CSV it will have to be. I don't have the application installed. Had some problems with that. I just checked again and I get this,
ERROR: Head "https://registry.gitlab.com/v2/champs-libres/geonature-citizen/taxhub/manifests/latest": denied: access forbidden
Not sure if that was the only problem, but it might be resolved if I could have access to that repository.
Well, let me know, if you'd like me to work on that or not. Always willing to try!