Read the statement by Michael Teeuw here.
How to load a <script> src = " " </script> into my mirror?
-
I’ve moved this thread into Development category, since conversation is now mostly related to your module development.
Thanks.
-
@morozgrafix I am referring back to your jsfiddle example: jsfiddle example, and you created an object called
csvDatawhich you refer to invar data = ...I was wondering how I would create a similar object, except with the file that I have in my directory. I couldn’t seem to find an HTML DOM function that would allow me to load the .csv file. I looked at the examples for the Compliments module, and they were in
json. So, I saw that I might be able to use Papa Parse to convert the .csv to json and then follow the similar route as the Compliments.My
d3approach loads the data from my csv onto my mirror, but the .css doesn’t seem to help it. It doesn’t appear in a table, and is very large (displaying in the upper left part of the mirror) even when I specified it to load in the middle.For some context, my .csv data table contains two columns of time-series data for which I want to sum together. And then I just want to output a sentence like, “Your sum is XXX.”
Thanks again for the clarifications. I’m slowly wrapping my head around the interaction of the HTML and Javascript, and I think I get confused when I see code split into HTML and Javascript, when the modules are written with it all together in the HTML DOM format.
-
It would be very difficult for me to guide you through building entire module without seeing the code and understanding what it is doing. That’s the reason why I’m only able to give you bits and pieces of information. One of the things that can help is for you to create a repo on GitHub with code that you got so far and then I can take a look and possibly offer some suggestions.
In your original post you mentioned that you wanted to display CSV data from the file in HTML table format which seems a bit different from your most recent requirement:
For some context, my .csv data table contains two columns of time-series data for which I want to sum together. And then I just want to output a sentence like, “Your sum is XXX.”
To achieve the above following very rough logic would need to be implemented:
- CSV file is created/updated in
/home/pi/MagicMirror/modules/datafeed/file.csv - When module is loaded JS creates a variable and reads contents of the CSV file into it (similar to what compliments module does)
- CSV data in that variable is parsed, sum of the values in one of the columns calculated and assigned to another variable (
sum). - DOM element is created and innerHTML of that element is updated with "Your sum is " +
sum - At given config interval steps 1-4 are repeated to get fresh set of data.
Is this close to something that you are trying to do?
(Above steps aren’t accounting for any error handling etc.)
BTW I don’t think you need to convert your CSV to JSON, especially if this is a simple 2 column file. Commenting out or removingxobj.overrideMimeType("application/json");fromcomplimentFileexample should be sufficient for CSV file reading.Thanks.
- CSV file is created/updated in
-
I also updated JSfiddle example with sum calculation logic: https://jsfiddle.net/morozgrafix/8oz1t9g4/2/
-
@morozgrafix Thanks for your outline. It is very, very helpful and appreciated. So I tried to take what you suggested to turn it into my code, and put it onto GitHub.
I created a GitHub repository that contains my
.css,.csv, anddatafeed.js. My areas of confusion (which you’ll probably see in the code) are around how I called in theremoteFilein my getDom. I am not sure if I did that correctly. https://github.com/brennn1/Magic-Mirror-Datafeed-ModuleMy
.csvis coming in the format which you’ll see on the Github - so each data point is separated by a new line. So I changed where you had theline.split(",")toline.split("\n").Do you see anything obvious that will cause me problems? I will go ahead and see what errors I get when I load it onto my mirror.
-
@nbrenn perfect!!! i can now see your full code and have better idea what you are trying to achieve.
Your data file is not really a CSV (Comma Separated Values) file, just a text file with values on each line, so you were correct to remove that
line.split(",")part. I already spotted a few things where your code will not work correctly. I will take a closer look at it later tonight and hopefully together we can get this resolved. -
@nbrenn I’ve reworked the code to follow that logic outline to get
Your sum is 37to show up in the middle of the MM. You can find a PR on GitHub or just look at my fork of your repo. Most of the code is commented and hopefully easy to follow. I also added some configuration options for better flexibility.
Since you didn’t need a table any longer, I took some shortcuts with processing the data and just calculating the sum of all numeric values from the file. You can add the module to the MM and then as it runs update the datafile with new values, MM should update in about a minute or whatever your
updateIntervalis set to.Let me know if you have any questions.
P.S. I didn’t run it on RasPi, but it should work without a problem. :fingers_crossed: Also if you are planning to make this module available for others it may be worth renaming it to
MMM-datafeedto follow the standard naming conventions. -
@morozgrafix This looks awesome! I will try to work with this over the next couple days and will let you know if I have any other questions! Thanks!
-
@nbrenn sounds good. Good luck!
-
@morozgrafix Everything is working very well so far!
I am updating my “Sum” daily, but I would like to be able to show the previous day’s sum. Could I store the value of the sum at 11:59pm, each evening, and then display that value in addition to the current day’s sum?
Additionally, how could I modify the line of code:
this.sum = lines.reduce((a, b) => a + b, 0);to allow for me to sum across multiple columns? So, if my .csv has 3 columns, I’d like the sum total of them.
-
This post is deleted! -
@nbrenn sorry swamped at work today. That line just takes all of the values from each line and adds them up. It will not work for multiple columns and true csv files. You would need to resort to logic that we had initially in JSfiddle. If I have time I will look into this later today.
-
@morozgrafix I think I can work around this by changing the SQL query (that generates my .csv) to include just one column. Then I can implement it as a new module, placed above the current one.
-
Does anyone know of the best documentation for adding an image/graphic into a mirror module?
For my example here, Mirror Datafeed and Sum Module, I am summing over the values in a column in a .csv and then outputting it.
I would like to show an image based on what that value is. As a crude example, if the sum is greater than 50, output a frown. If it is less than 50, show a smiling face. What’s the best documentation for putting in graphics that will work on the mirror?
-
@nbrenn I’m sure there are many ways but here’s one … :laughing:
I borrowed this from strawberrys modules…
var awayLogo = document.createElement("span"); var awayIcon = document.createElement("img"); awayIcon.src = this.file("icons/" + game.awayTeam + ".png"); awayLogo.appendChild(awayIcon); large.appendChild(awayLogo);he’s getting the name from parsing xml and the away team name is for example ‘bal’… so his png in his icon folder is named ‘bal.png’ and here he’s calling it to show… that’s one way :)
You can also look at morozgrafix’s MMM-Horoscope to see another … hope it helps!
-
@cowboysdude Thanks for the example!
Is there a specific styling that needs to be used for the images? For example, I would need the image to have the typical white/greyish hue to it.
-
@nbrenn I believe you can do it with a classid…
var gameTemp = document.createElement("div"); gameTemp.classList.add("gametemp");Then add it your css file:
.MMM-NFLweather .gametemp { color: #10B1C8; } -
@cowboysdude this will not work for images
therefore you have to add a filter
-webkit-filter: grayscale(100%); -
@strawberry-3.141 Yeap you’re right… he wanted greyscale… ok got it!
-
@cowboysdude @strawberry-3-141
I’m having trouble getting my icon to load. My
getDomlooks like the following:getDom: function() { var wrapper = document.createElement("div"); if (!this.loaded) { wrapper.innerHTML = this.translate("Loading..."); wrapper.className = "dimmed light small"; return wrapper; } wrapper.className = "datafeed"; var myLogo = document.createElement("span"); var myIcon = document.createElement("img"); myLogo.classList.add("myLogo"); myIcon.src = this.file("my_logo.png"); myLogo.appendChild(myIcon); wrapper.appendChild(myLogo); wrapper.innerHTML = "Today, your sum is " + this.sum ; return wrapper; },
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login