How to import an image in Fable? - f#

I already have all the webpack loaders, so all I need to do is this, but in F#:
import loading from "../images/loading.gif";
This should be an easy thing, I am just lost in the docs and I mostly find info about [<Import>] attribute which seems to be for something else...

In the module Fable.Core.JsInterop we do provide helpers for dealing with import.
Source
I think the one you are looking for is:
/// Works like `ImportAttribute` (same semantics as ES6 imports).
/// You can use "*" or "default" selectors.
let import<'T> (selector: string) (path: string):'T = jsNative
And you can use it like that:
open Fable.Core.JsInterop
let loading : string = import "*" "../images/loading.gif"
I set the type to string because WebPack seems to give you the file URL.

Is there a specific reason you need to do the import for the image?
You can easily display the images in your view (eg. in elmish) without having to import the images, as long as your url's are valid.
let view model dispatch =
div [] [
img [ Class "mr-3 mt-1"
Style [ Width 33% ; Height "80px" ]
Src "../images/loading.gif"
Alt cat.Text; Placeholder "image" ]
]

Related

Require json file dynamically in react-native (from thousands of files)

I googled so far and tried to find out the solution but not yet.
I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.
Please advise me the best approach to handle such case.
Background
I have thousand of json files that containing app data, and declared all the file path dynamically like below:
export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....
As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.
For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.
I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.
Any suggestions please.
Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:
// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}
//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');
const requiredFile = require(myFile);
then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.

URL Mapping - Replacing characters in a parameter pulled from a database

I am currently trying to figure out, how to modify the parameter being integrated into the URL Mapping I am using.
static mappings =
{
"/$controller/$action?/$id?/(.$format)?"
{
constraints {
// apply constraints here
}
}
name test1: "/.../$title/..."{
controller = "study"
action = "st_show"
}
name test2: "/.../$title/..."{
controller = "search"
action = "se_show"
}
The parameter $title is pretty much a dataset, which is pulled from a database and which will get transmitted in the following format [ this is a title ]. So there are square brackets in front and behind the string and words are seperated through blanks.
If I am creating a link through g:link now with the params nested in, it gets put into the url as it is pulled from the database. What I am attempting is to create SEO-URLs, which will present a certain title of a publication devided by hyphens instead of url-encoded "%20".
Until now, I was able to generate dynamic urls looking like this:
http://localhost:8080/projectname/show/%5BAllgemeine%20Bevölkerungs[...]/782/...PARAMS...
Furthermore I already implemented it through JQuery, though it should be static and users should be able to copy the link to open up the page themselves - that wouldn't be possible when changing the url client-side while loading up the page.
Is there a way to define a function with something like replaceAll.(' ', '-'), which can be invoked onto the parameter in the mapping to replace blanks with hyphens and f.e. square brackets with an empty character?
That's pretty much, what I wasn't able to come by through the documentation.
Thank you already in advance for your help!
I managed to solve my problem by creating a service with a function containing a regex and executing this function onto the parameter title in my g:link, which I firstly converted to a string, which gets passed to the function.
<g:link controller="study" action="st_show" params="[data: data, ... title: ConversionService.convert(fieldValue(bean: path).toString(), ... data: data)]"></g:link>
And the function in the ConversionService
public static String convert(String title){
title = title.replaceAll("\\s", "-").replaceAll("[^0-9a-zA-Z\\-]", "");
return title;
}

Grails excel-import plugin Numbers populated as real with exponent

I am trying to import a ms-excel 2007 sheet using excel-import plugin. It was simple to integrate with my project and I found it working as expected until I noticed that the number values in the cells are populated as real numbers with exponent.
For example if the cell contains value 9062831150099 -(populated as)->9.062831150099E12 i.e.
A |
_____________________
Registration Number |
____________________
9062831150099
Is populated as: [RegNum:9.062831150099E12]
Anyone could suggest me how I can change this representation back to its original format keeping its type as number?
Missed it at the first attempt but I figured out how to to achieve it:
When invoking the key methods (the ones that process cellMap and columnMaps) for example List bookParamsList = excelImportService.columns(workbook, CONFIG_BOOK_COLUMN_MAP) or Map bookParams = excelImportService.cells(workbook, CONFIG_BOOK_CELL_MAP )
There is also ability to pass in configuration information, i.e. to specify what to do if a cell is empty (i.e. to provide a default value), to make sure a cell is of expected type, etc.
So in my case I created a configuration parameter Map of properties of the object and passed it to the above methods. i.e.
Class UploadExcelDataController {
def excelImportService
static Map CONFIG_BOOK_COLUMN_MAP = [
sheet:'Sheet1',
startRow: 2,
columnMap: [
'A':'registrationNumber',
'B':'title',
'C':'author',
'D':'numSold',
]
]
static Map configBookPropertyMap = [
registrationNumber: ([expectedType: ExpectedPropertyType.IntType, defaultValue:0])
]
def uploadFile(){
...
List bookParamsList = excelImportService.columns(workbook, CONFIG_BOOK_COLUMN_MAP,configBookPropertyMap )
...
}
}

Cannot read properties from custom config

I am using Grails 2.2.1. I want to put some configuration into another file besides Config.groovy.
I put it in a file called My-config.groovy. It's contents are short and simple
uten {
currency="USD"
user="smacko"
}
I add the following to the top of my Config.groovy
grails.config.locations = [ "classpath:My-config.groovy"]
In my code when I do:
String user = grailsApplication.config.uten["user"]
At runtime, I check in the debugger and the user variable is just a String with the value groovy.util.ConfigObject. What are my doing wrong?
If you want uten to be a map your the config should look like this:
uten = [
currency: "USD",
user: "smacko"
]
Personally I am skeptical about using a file name with -. Your approach is correct in using the config file. If there is no resistance in changing the file name to MyConfig.groovy then this option will work effortlessly:
grails.config.locations = [MyConfig]

Deedle - create empty list and series

I am new to F#, looking at it as an alternative to Matlab.
In reference to this question, how can I create an empty Serie and an empty Frame.
If I did not just miss it, why an empty Serie or Frame has not been designed in the library,
something like list.empty ?
Adding Frame.empty and Series.empty is a great suggestion. I think these should be in the library and I'm adding an issue to GitHub to make sure they get added.
In the meantime, you should be able to use something like this:
let empty : Series<int, float> = series []
let empty : Frame<int, string> = frame []
Note that I had to add type annotations - from my code snippet, the compiler cannot figure out what is the type of keys and values, so I had to explicitly specify that (but if you use the values as arguments to other functions, then this should not be needed).
The second line does not actually work in the current version, because of a bug (oops!) so you can use Frame.ofRows instead, which works fine:
let frame : Frame<int, string> = Frame.ofRows []
EDIT: The bug is now fixed in version 0.9.11-beta

Resources