My background is python using pandas for data wrangling and analysis purposes. Now I want to use Elm as a frontend to enable user interaction.
My idea is to use
- Elm as frontend (just have a nice user interface with inputs and drop downs etc.)
- Flask / Pyhton as backend (all the data wrangling etc.)
Given I am just experimenting and I lack experience using “web apps” I am really struggling to have elm and flask communicate with each other. Unfortunately, I cannot find any good and simple examples anywhere. This may, however, also be due to my lack of understanding.
I started to build a very simplified webapp that should be able to do the following:
- input a value in an input field
- sent this input to the flask server (http.post?)
- have python transform the input
- give the transformed input back and show it in the output field (http.get?)
My below code already fails at the second step. Given my lack of experience I don’t even know if the issue is at the Elm end or in the flask end. The Elm code compiles correctly.
module SimpleTest exposing (..)
import Html exposing (..)
import Html.Attributes as Attr
import Html.Events exposing (onClick, onInput)
import Browser
import Http
import Json.Decode as Decode exposing (string, Decoder)
import Json.Encode as Encode
type alias Model =
{ input : String
, output : String
, errorMessage : String
}
initialModel : Model
initialModel =
{ input = ""
, output = ""
, errorMessage = "No Error"
}
view : Model -> Html.Html Msg
view model =
div [][ div [][ text "Input"
, input [ Attr.name "InputField"
, onInput GetInput
][]
, button [Attr.name "Button", onClick PressedOk][text "OK"]
]
, div [][text ""]
, div [][text "Output"
, input [Attr.name "OutputField"][]
]
, h2 [][text "Debugger"]
, (text << Debug.toString) model
]
type Msg =
PressedOk
| GetInput String
| SentInput (Result Http.Error String)
| ReceiveOutput
inputEncoder : String -> Encode.Value
inputEncoder inputStr =
Encode.object [("input", Encode.string inputStr)]
outputDecoder : Decoder String
outputDecoder =
Decode.field "output" Decode.string
sentData : String -> Cmd Msg
sentData inputStr =
Http.post
{ url = "http://localhost:8989/data"
, body = Http.jsonBody (inputEncoder inputStr)
, expect = Http.expectString SentInput
}
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
GetInput newInput->
({model | input = newInput}, Cmd.none)
PressedOk ->
(model, sentData model.input)
SentInput inputData ->
case inputData of
Ok newOutput ->
({model | output = newOutput }, Cmd.none)
Err error ->
({model | errorMessage = Debug.toString error}, Cmd.none)
ReceiveOutput ->
(model, Cmd.none)
init : () -> (Model, Cmd Msg)
init _ =
(initialModel, Cmd.none)
main : Program () Model Msg
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
Could someone point me in the right direction how to achieve the four steps mentioned above?
Thanks for your help and your patience with a Newbie.