Skip to content
Snippets Groups Projects
Commit 6d10416a authored by Sabed Ger's avatar Sabed Ger
Browse files

Die csv-Daten werden nun decodiert und in einer einfachen Tabelle ausgegeben

parent bd5bc264
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@ module StudentsPerformanceInExams exposing (main)
-- elm stuff
import Browser
import Html exposing (Html, text, pre, div)
import Html exposing (Html, text, pre, div, table, tr, th, td)
import Http
import Dict
import Maybe.Extra
......@@ -38,7 +38,7 @@ main =
-- MODEL
type alias Model = { httpState : HttpState
, fullTexts : List {description: String, data: String}
, data : List OldData
, data : List Data
, error : String
}
......@@ -58,9 +58,9 @@ type alias Data = { gender: String
, parentEducation: String
, lunch: String
, testPreparation: String
, mathScore: String
, readingScore: String
, writingScore: String
, mathScore: Int
, readingScore: Int
, writingScore: Int
}
filesToLoad =
......@@ -101,10 +101,7 @@ updateModel msg model =
in
{ model | httpState = Success
, fullTexts = checkedFullTexts
, data = prepareData <|
List.map
(\data -> OldData data.description <| csvString_to_data data.data)
checkedFullTexts
, data = csvString_to_data <| Maybe.withDefault "" <| List.head <| List.map .data checkedFullTexts
}
else{ model | httpState = Loading (filesLeft-1)
, fullTexts = {description = file, data = fullText}::model.fullTexts
......@@ -129,7 +126,36 @@ view model =
text ("Loading... "++(String.fromInt index)++" left")
Success ->
text <| Maybe.withDefault "Nothing here" <| List.head <| List.map .data model.fullTexts
div []
[ table []
(List.concat
[[ tr []
[ th [] [text "gender"]
, th [] [text "race"]
, th [] [text "parentEducation"]
, th [] [text "lunch"]
, th [] [text "testPreparation"]
, th [] [text "mathScore"]
, th [] [text "readingScore"]
, th [] [text "writingScore"]
]
]
, List.map (\data ->
tr []
[ td [] [text data.gender ]
, td [] [text data.race ]
, td [] [text data.parentEducation]
, td [] [text data.lunch ]
, td [] [text data.testPreparation]
, td [] [text <| String.fromInt data.mathScore ]
, td [] [text <| String.fromInt data.readingScore]
, td [] [text <| String.fromInt data.writingScore]
]
) model.data
]
)
--, text <| Maybe.withDefault "Nothing here" <| List.head <| List.map .data model.fullTexts
]
{-
let rawlevel : Int -> List (Int,Int)
......@@ -260,22 +286,34 @@ checkInputFormat input =
in {input | data = dataInCorrectFormat }
else input
csvString_to_data : String -> List ( String, Maybe Float )
csvString_to_data : String -> List Data
csvString_to_data csvRaw =
Csv.parse csvRaw
|> Csv.Decode.decodeCsv decodeStockDay
|> Csv.Decode.decodeCsv decodeStudentsPerformanceInExams
|> Result.toMaybe
|> Maybe.withDefault []
decodeStockDay : Csv.Decode.Decoder (( String, Maybe Float ) -> a) a
decodeStockDay =
Csv.Decode.map (\a b -> ( a, Just b ))
(Csv.Decode.field "Date" Ok
|> Csv.Decode.andMap
(Csv.Decode.field "Open"
(String.toFloat >> Result.fromMaybe "error parsing string")
)
)
{-gender: String
, race: String
, parentEducation: String
, lunch: String
, testPreparation: String
, mathScore: Int
, readingScore: Int
, writingScore: Int
-}
decodeStudentsPerformanceInExams : Csv.Decode.Decoder ( Data -> a ) a
decodeStudentsPerformanceInExams =
Csv.Decode.map (\gender race pE lunch test math read write -> Data gender race pE lunch test math read write )
(Csv.Decode.field "gender" Ok
|> Csv.Decode.andMap (Csv.Decode.field "race/ethnicity" Ok)
|> Csv.Decode.andMap (Csv.Decode.field "parental level of education" Ok)
|> Csv.Decode.andMap (Csv.Decode.field "lunch" Ok)
|> Csv.Decode.andMap (Csv.Decode.field "test preparation course" Ok)
|> Csv.Decode.andMap (Csv.Decode.field "math score" (String.toInt >> Result.fromMaybe "error parsing math score" ))
|> Csv.Decode.andMap (Csv.Decode.field "reading score" (String.toInt >> Result.fromMaybe "error parsing reading score"))
|> Csv.Decode.andMap (Csv.Decode.field "writing score" (String.toInt >> Result.fromMaybe "error parsing writing score"))
)
prepareData : List OldData -> List OldData
prepareData rawData =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment