-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code example generation for search page #381
base: main
Are you sure you want to change the base?
Changes from all commits
b973c18
36e86dc
d1cdb0a
2598ae2
904cb09
7942f01
9f02d73
33d7c2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<template> | ||
<div class="codebox"> | ||
<pre v-highlightjs> | ||
<code :class="language" :id="componentId">{{ code }}</code> | ||
</pre> | ||
<CopyButton variant="primary" :copyText="code">{{ $t('copy') }}</CopyButton> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: "CodeBox", | ||
components: { | ||
CopyButton: () => import("./CopyButton.vue") | ||
}, | ||
props: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both props should probably be required. |
||
code: { | ||
type: String, | ||
default: "", | ||
}, | ||
|
||
language: { | ||
type: String, | ||
default: "", | ||
}, | ||
}, | ||
data() { | ||
return { | ||
componentId: `${this.language}Content` | ||
}; | ||
}, | ||
}; | ||
</script> | ||
|
||
<style scoped> | ||
.codebox { | ||
border: 1px solid #ccc; | ||
padding: 16px; | ||
border-radius: 8px; | ||
} | ||
.codebox button { | ||
background-color: #007BFF; | ||
color: #FFF; | ||
padding: 5px 15px; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
transition: background-color 0.3s; | ||
} | ||
|
||
.codebox button:hover { | ||
background-color: #0056b3; | ||
} | ||
.codebox pre, .codebox code { | ||
padding: 0; | ||
margin: 0; | ||
} | ||
|
||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<template> | ||
<b-tabs> | ||
<b-tab title="Python"> | ||
<CodeBox :code="pythonCode" language="python" /> | ||
</b-tab> | ||
<b-tab title="Javascript"> | ||
<CodeBox :code="javascriptCode" language="javascript" /> | ||
</b-tab> | ||
</b-tabs> | ||
</template> | ||
|
||
<script> | ||
import { BTabs, BTab } from 'bootstrap-vue'; | ||
export default { | ||
name: "SearchCode", | ||
components: { | ||
BTab, | ||
BTabs, | ||
CodeBox: () => import('./CodeBox.vue'), | ||
}, | ||
props: { | ||
catalogHref: { | ||
type: String, | ||
default: '', | ||
}, | ||
filters: { | ||
type: Object, | ||
default() { | ||
return {}; | ||
}, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
componentId: `${this.language}Content`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems unsused |
||
pythonCode: null, | ||
javascriptCode: null, | ||
rCode: null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems unsused |
||
}; | ||
}, | ||
watch: { | ||
filters: { | ||
deep: true, | ||
handler() { | ||
this.updateCode(); | ||
} | ||
} | ||
}, | ||
created() { | ||
this.updateCode(); | ||
}, | ||
methods: { | ||
dedent(str) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This dedents a bit too much, the JS code is not indented at all, which looks rather weird. |
||
const lines = str.split('\n').map(line => line.trim()); | ||
return lines.join('\n').trim(); | ||
}, | ||
filterString() { | ||
let obj = this.filters || {}; | ||
for (let key in obj) { | ||
if (obj[key] === null || (Array.isArray(obj[key]) && obj[key].length === 0)) { | ||
delete obj[key]; | ||
} | ||
} | ||
return JSON.stringify(obj); | ||
}, | ||
generatePython() { | ||
return this.dedent(`from pystac_client import Client | ||
|
||
# Connect to STAC API | ||
stac_endpoint = '${this.catalogHref}' | ||
client = Client.open(stac_endpoint) | ||
|
||
# Build query | ||
query = ${this.filterString()} | ||
|
||
# Perform search | ||
search_result = client.search(query)`); | ||
}, | ||
generateJavascript() { | ||
return this.dedent(`// Define the STAC API endpoint | ||
const STAC_ENDPOINT = '${this.catalogHref}'; | ||
|
||
// Define your search parameters | ||
const searchParams = ${this.filterString()}; | ||
|
||
// Perform the search | ||
fetch(STAC_ENDPOINT, { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify(searchParams) | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
console.log("STAC search results:", data); | ||
}) | ||
.catch(error => { | ||
console.error("Error fetching STAC data:", error); | ||
});`); | ||
}, | ||
updateCode() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be a bit better to only generate what's shown, not everything upfront, especially if we extend this. |
||
this.pythonCode = this.generatePython(); | ||
this.javascriptCode = this.generateJavascript(); | ||
} | ||
}, | ||
}; | ||
</script> | ||
|
||
<style scoped> | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,13 +112,17 @@ | |
<b-card-footer> | ||
<b-button type="submit" variant="primary">{{ $t('submit') }}</b-button> | ||
<b-button type="reset" variant="danger" class="ml-3">{{ $t('reset') }}</b-button> | ||
<b-button type="button" variant="info" class="ml-3" v-b-modal.code>{{ $t('exampleCode') }}</b-button> | ||
</b-card-footer> | ||
</b-card> | ||
<b-modal id="code" :title="$t('exampleCode')" size="lg"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The model shows a Cancel and OK button, it seems one Close button is enough? |
||
<SearchCode :filters="query" :catalogHref="searchLink.href" /> | ||
</b-modal> | ||
</b-form> | ||
</template> | ||
|
||
<script> | ||
import { BBadge, BDropdown, BDropdownItem, BForm, BFormGroup, BFormInput, BFormCheckbox, BFormRadioGroup } from 'bootstrap-vue'; | ||
import { BBadge, BDropdown, BDropdownItem, BForm, BFormGroup, BFormInput, BFormCheckbox, BFormRadioGroup, BModal, VBModal } from 'bootstrap-vue'; | ||
import Multiselect from 'vue-multiselect'; | ||
import { mapGetters, mapState } from "vuex"; | ||
import refParser from '@apidevtools/json-schema-ref-parser'; | ||
|
@@ -176,12 +180,17 @@ export default { | |
BFormInput, | ||
BFormCheckbox, | ||
BFormRadioGroup, | ||
BModal, | ||
QueryableInput: () => import('./QueryableInput.vue'), | ||
Loading, | ||
Map: () => import('./Map.vue'), | ||
SearchCode: () => import('../components/SearchCode.vue'), | ||
SortButtons: () => import('./SortButtons.vue'), | ||
Multiselect | ||
}, | ||
directives: { | ||
'b-modal': VBModal | ||
}, | ||
mixins: [ | ||
ApiCapabilitiesMixin, | ||
DatePickerMixin | ||
|
@@ -202,6 +211,10 @@ export default { | |
value: { | ||
type: Object, | ||
default: () => ({}) | ||
}, | ||
searchLink: { | ||
type: Object, | ||
default: () => ({}) | ||
} | ||
}, | ||
data() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be registered so that it doesn't import into the global bundle? Ideally this would only be loaded if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could say a bit more - I'm new to Vue, so some of the concepts here require me to squint a bit more than others might
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should only be imported in the CodeBox component and not use Vue.use. I'm not sure whether this library supports this though.
Similarly, the Bootstrap Badges should also be imported individually where the are used. I think there should be examples around in the code if you search for the BBadge component.
I'm a bit busy right now so can't elaborate further at this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I follow you - just been using python and jvm lingo for long enough that 'global bundle' tripped me up. Also, I think the badge plugin is from prior work and I accidentally shifted its order to the diff's confusion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Global Bundle is probably also not the right terminology. I think better is "app bundle" or "initially loaded files". I pretty much just want that it is lazily loaded when needed, not everytime the page loads. :-)