-
Notifications
You must be signed in to change notification settings - Fork 159
Code example generation for search page #381
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
base: main
Are you sure you want to change the base?
Changes from 2 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 |
---|---|---|
|
@@ -35,6 +35,11 @@ import { | |
import "bootstrap/dist/css/bootstrap.css"; | ||
import "bootstrap-vue/dist/bootstrap-vue.css"; | ||
|
||
import VueHighlightJS from "vue-highlightjs"; | ||
import highlight from 'highlight.js'; | ||
import 'highlight.js/lib/languages/python'; | ||
import 'highlight.js/styles/github.css'; | ||
|
||
import ErrorAlert from './components/ErrorAlert.vue'; | ||
import StacHeader from './components/StacHeader.vue'; | ||
|
||
|
@@ -49,8 +54,9 @@ import { getBest, prepareSupported } from './locale-id'; | |
Vue.use(AlertPlugin); | ||
Vue.use(ButtonGroupPlugin); | ||
Vue.use(ButtonPlugin); | ||
Vue.use(BadgePlugin); | ||
Vue.use(BadgePlugin) | ||
Vue.use(CardPlugin); | ||
Vue.use(VueHighlightJS); | ||
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. 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 commentThe 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 commentThe 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 commentThe 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 commentThe 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. :-) |
||
Vue.use(LayoutPlugin); | ||
Vue.use(SpinnerPlugin); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<template> | ||
<div class="codebox"> | ||
<pre v-highlightjs> | ||
<code :class="language" :id="componentId"> | ||
{{ code }} | ||
</code> | ||
</pre> | ||
<button ref="copyButton" :data-clipboard-target="'#' + componentId">Copy</button> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import Clipboard from 'clipboard'; | ||
|
||
export default { | ||
name: "CodeBox", | ||
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: String, | ||
language: String | ||
}, | ||
data() { | ||
return { | ||
componentId: `${this.language}Content` | ||
} | ||
}, | ||
mounted() { | ||
new Clipboard(this.$refs.copyButton); | ||
}, | ||
}; | ||
</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,122 @@ | ||
<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-tab title="R"> | ||
<CodeBox :code="rCode" language="r" /> | ||
</b-tab> | ||
</b-tabs> | ||
</template> | ||
|
||
<script> | ||
import { BTabs, BTab } from 'bootstrap-vue'; | ||
export default { | ||
name: "SearchCode", | ||
props: { | ||
catalogHref: String, | ||
filters: Object, | ||
}, | ||
components: { | ||
BTab, | ||
BTabs, | ||
CodeBox: () => import('./CodeBox.vue'), | ||
}, | ||
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 |
||
} | ||
}, | ||
methods: { | ||
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 ` | ||
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 ` | ||
// 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); | ||
}); | ||
` | ||
}, | ||
generateR() { | ||
return ` | ||
from pystac_client import Client | ||
|
||
# Connect to STAC API | ||
stac_api_url = '${this.catalogHref}' | ||
client = Client.open(stac_api_url) | ||
|
||
# Build query | ||
query = ${this.filterString()} | ||
|
||
# Perform search | ||
search_result = client.search(query) | ||
` | ||
}, | ||
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() | ||
this.rCode = this.generateR() | ||
} | ||
}, | ||
watch: { | ||
filters: { | ||
deep: true, | ||
handler() { | ||
this.updateCode(); | ||
} | ||
} | ||
}, | ||
mounted() { | ||
this.updateCode(); | ||
}, | ||
}; | ||
</script> | ||
|
||
<style scoped> | ||
</style> |
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.
We already use v-clipboard, can you re-use that instead of a new/this dependency?