I am rendering Hamlet templates using the Runtime module. The following works as promised with the example data:
let hamletDataMap = Map.fromList
[ ("name", "Michael")
, ("hungry", toHamletData True) -- always True
, ("foods", toHamletData
[ "Apples"
, "Bananas"
, "Carrots"
])
]
But I can't see any way for me to render nested data. For instance if I have a list of metadata of various fruits, I would like to do something like:
let hamletDataMap = Map.fromList
[ ("name", "Michael")
, ("hungry", toHamletData True) -- always True
, ("fruits", toHamletData
[ [ ("name", "apple")
, ("taste", "sour")
]
, [ ("name", "..")
, ("taste", "...")
]
])
]
In the Text.Hamlet.RT there is the HDList [HamletMap] which looks kindof strange but still promising. I can creates instances of HDList but it gives me type mismatch HamletData with actual type RT.HamletData url0.
I'm thankful for any ideas or suggestions.
Related
I am trying to build a simple web dashboard with plotly and dash. Here is the structure I am trying to get:
app = dash.Dash(__name__)
def Build_home_page():
"""
build_welcome_banner [summary]
[extended_summary]
Returns
-------
[type]
[description]
""" ""
return [
html.Div(
id="banner",
className="box",
children=[
html.Div(
id="welcome-user",
children=[
html.H1("WELCOME TO THE DATA SCIENCE"),
html.H1("AND MACHINE LEARNING PLATFORM"),
html.Hr(className="tickline"),
html.Br(),
html.P(id="welcome-text1",
children= [
"That provides you with the tools that allow you to explore your data seemlessly and"
]),
html.P(id= "welcome-text2",
children= [
"get actionable insights from your data and allows you to easily apply Machine learning models to make predictions with your data"
]),
],
),
],
),
html.Div(
id= "features",
className= "features-box-model",
children=[
html.Div(
id= "feature-text",
children= [
html.Div("Features"),
],
),
],
),
]
app.layout = html.Div(
id="get-started-page",
children=[
Build_home_page(),
],
)
if __name__ == '__main__':
app.run_server(debug=True,
use_reloader=False,
port=8080)
Then I got the following error: The children property of a component is a list of lists, instead of just a list. Check the component that has the following contents, and remove one of the levels of nesting:
What could be wrong? I checked my list and seems ok! Each children is packed with a list around it. I cant figure out where the issue is
You have wrapped the children of the most outer Div in a list twice, both inside the Build_home_page and in the layout definition, you should remove one of them. Removing the latter, the layout definition would be
app.layout = html.Div(
id="get-started-page",
children=Build_home_page()
)
I have model
type alias Model =
{
url : String
, devices : List Device
, input : String
, isFilter : Bool
, deviceSuggestion : Maybe Device
}
type alias Device =
{
status : String
, license_plate : String
, device_id : String
}
This is how i update the data but i have no idea how to do for filter after i type something in the search box
update msg model =
case msg of
GotResult result ->
case result of
Ok devices ->
( { model | devices = devices }, portDevice devices )
Err devices ->
( { model | error = Just "Error" }, Cmd.none )
Tick _ ->
( model, fetchUrl )
InputChange newInput ->
( { model | input = newInput //function filter here?}, Cmd.none)
SearchFunction ->
({ model | input = "" }, toJS model.input)
This is my rendering View
renderPosts : Model -> Html Msg
renderPosts model =
div []
[ h3 [] [ text "Filter List" ] ,
, input [ type_ "text"
, placeholder "Searching for devices"
, onInput InputChange
, value model.input
, id "inputDevices"
] []
--, checkListFunction model //check and filter list function here as well?
, button [ onClick SearchFunction , id "searchDevice"] [ text "Search" ]
,
div []
([ text "Device List" ] ++ List.map (\device -> renderPost device) model.devices) //display function
]
I want the output is like when i type the 1234 in searchbox then it detects and filter the below list with the license_plate in devices list.
**I tried the list.filter but it allows compare list to list.
**I tried String.contains but i need 2 strings. It gives error because input box is String and license_plate is List
Any help is appreciate...
https://www.w3schools.com/howto/howto_js_filter_lists.asp <<< Example of output
Below is an example of modifying your renderPosts function with a filteredDevices binding, showing how you could apply the filter:
renderPosts : Model -> Html Msg
renderPosts model =
let
filteredDevices =
List.filter (\device => String.contains model.input device.license_plate) model.devices
in
div []
[ h3 [] [ text "Filter List" ] ,
...
div []
([ text "Device List" ] ++ List.map (\device -> renderPost device) filteredDevices)
]
Given a tree based of list of lists:
tree = [
"A",
[
"B1",
[
"C"
]
],
[
"B2",
[
"C",
[
"D1",
[
"E1"
],
[
"E2"
]
],
[
"D2"
]
]
]
]
I want to get the full path to all child elements as concatenated string in a list.
result = [
'A>B1>C',
'A>B2>C>D1>E1',
'A>B2>C>D1>E2',
'A>B2>C>D2'
]
The delimiter > is changeable.
I tried different things with recursion and yield. But my head is burning.
Try this it works.
def leave_paths(current):
if len(current) == 1:
return [current]
# Take all branches, get the paths for the branch, and prepend
return [[current[0]] + path for branch in current[1:] for path in leave_paths(branch)]
output = ['>'.join(sub_list) for sub_list in leave_paths(s)]
print(output)
Output
['A>B1>C', 'A>B2>C>D1>E1', 'A>B2>C>D1>E2', 'A>B2>C>D2']
Smth like this
def findLeaves(tree):
name=tree[0]
If len(tree)==1:
return [name]
else:
return [name+"->"+path for branch in tree[1:] for path in findLeaves(branch)]
Using HQL queries I've been able to generate the following map, where the keys represent the month number constant defined in java.util.Calendar, and every value is a map:
[
0:[ client_a:[order1, order2, order3]],
1:[ client_b:[order4], client_c:[order5, order6], client_d:[order7]],
2:[ client_e:[order8, order9], client_f:[order10]]
]
order1, order2, ... are instances of a domain class called Order:
class Order {
String description
Date d
int quantity
}
Now I've got that structure containing orders that belong to some specific year, but I don't really care about the Order object itself. I just want the sum of the quantities of all the orders of each month. So the structure should look something like this:
[
0:[ client_a:[321]],
1:[ client_b:[9808], client_c:[516], client_d:[20]],
2:[ client_e:[22], client_f:[10065]]
]
I don't mind if the values are lists of one element or not lists at all. If this is possible, it would be fine anyway:
[
0:[ client_a:321 ],
1:[ client_b:9808, client_c:516, client_d:20 ],
2:[ client_e:22, client_f:10065 ]
]
I know I have to apply something like .sum{it.quantity} to every list of orders to get the result I want, but I don't know how to iterate over them as they are nested within another map.
Thank you.
Here You go:
class Order {
String description
Date d
int quantity
}
def orders = [
0:[ client_a:[new Order(quantity:1), new Order(quantity:2), new Order(quantity:3)]],
1:[ client_b:[new Order(quantity:4)], client_c:[new Order(quantity:5), new Order(quantity:6)], client_d:[new Order(quantity:7)]],
2:[ client_e:[new Order(quantity:8), new Order(quantity:9)], client_f:[new Order(quantity:10)]]
]
def count = orders.collectEntries { k, v ->
def nv = v.collectEntries { nk, nv ->
[(nk): nv*.quantity.sum()]
}
[(k):(nv)]
}
assert count == [0:[client_a:6], 1:[client_b:4, client_c:11, client_d:7],2:[client_e:17, client_f:10]]
def map = [
0:[ client_a:[[q: 23], [q: 28], [q: 27]]],
1:[ client_b:[[q: 50]], client_c:[[q: 100], [q: 58]], client_d:[[q: 90]]],
2:[ client_e:[[q: 48], [q: 60]], client_f:[[q: 72]]]
]
map.collectEntries { k, v ->
[ k, v.collectEntries { key, val ->
[ key, val*.q.sum() ]
} ]
}
you can also use val.sum { it.q } instead of val*.q.sum()
I am adding a list of strings to a QList Widget like this:
myList.addItems( [ 'item1' , 'item2' , 'item3' ] )
By default the list aligns these to the left but I want to get them in the center of the list.
Any Ideas?
If you create a QListWidgetItem() you can call its setTextAlignment() method and pass Qt.AlignHCenter:
item_text_list = [ 'item1' , 'item2' , 'item3' ]
for item_text in item_text_list:
item = QListWidgetItem(item_text)
item.setTextAlignment(Qt.AlignHCenter)
myList.addItem(item)
Docs:
QListWidgetItem, Qt.AlignmentFlag