Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"v-clipboard": "^3.0.0-next.1",
"vue": "^2.6.12",
"vue-chartjs": "^5.2.0",
"vue-highlightjs": "^1.3.3",
"vue-i18n": "^8.28.2",
"vue-multiselect": "^2.1.6",
"vue-read-more-smooth": "^0.1.8",
Expand Down
9 changes: 7 additions & 2 deletions src/StacBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ import {
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";

import VueHighlightJS from "vue-highlightjs";
import 'highlight.js/lib/languages/python';
import 'highlight.js/styles/github.css';

import ErrorAlert from './components/ErrorAlert.vue';
import StacHeader from './components/StacHeader.vue';

Expand All @@ -49,8 +53,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);
Copy link
Collaborator

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.

Copy link
Author

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

Copy link
Collaborator

@m-mohr m-mohr Oct 18, 2023

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.

Copy link
Author

@moradology moradology Oct 18, 2023

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

Copy link
Collaborator

@m-mohr m-mohr Oct 18, 2023

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. :-)

Vue.use(LayoutPlugin);
Vue.use(SpinnerPlugin);

Expand Down Expand Up @@ -392,4 +397,4 @@ export default {
@import '~bootstrap-vue/src/index.scss';
@import "./theme/page.scss";
@import "./theme/custom.scss";
</style>
</style>
59 changes: 59 additions & 0 deletions src/components/CodeBox.vue
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: {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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>
109 changes: 109 additions & 0 deletions src/components/SearchCode.vue
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`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unsused

pythonCode: null,
javascriptCode: null,
rCode: null
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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>
15 changes: 14 additions & 1 deletion src/components/SearchFilter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Copy link
Collaborator

Choose a reason for hiding this comment

The 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';
Expand Down Expand Up @@ -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
Expand All @@ -202,6 +211,10 @@ export default {
value: {
type: Object,
default: () => ({})
},
searchLink: {
type: Object,
default: () => ({})
}
},
data() {
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/texts.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"serverError": "The server encountered an issue. @:errors.contactProvider",
"unauthorized": "The request lacks credentials, e.g. an API token. Please provide your credentials and try again."
},
"exampleCode": "Example Code",
"featureExperimental": "This feature is still experimental and may give unexpected results!",
"fullscreen": {
"exit": "Exit Fullscreen",
Expand Down
17 changes: 10 additions & 7 deletions src/views/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
<b-tabs v-model="activeSearch">
<b-tab v-if="collectionSearch" :title="$t('search.tabs.collections')">
<SearchFilter
:parent="parent" title="" :value="collectionFilters" type="Collections"
:parent="parent" title="" :value="collectionFilters" :searchLink="searchLink"
type="Collections"
@input="setFilters"
/>
</b-tab>
<b-tab v-if="itemSearch" :title="$t('search.tabs.items')">
<SearchFilter
:parent="parent" title="" :value="itemFilters" type="Global"
:parent="parent" title="" :value="itemFilters" :searchLink="searchLink"
type="Global"
@input="setFilters"
/>
</b-tab>
Expand All @@ -25,9 +27,11 @@
<b-alert v-else-if="data === null" variant="info" show>{{ $t('search.modifyCriteria') }}</b-alert>
<b-alert v-else-if="results.length === 0" variant="warning" show>{{ $t('search.noItemsFound') }}</b-alert>
<template v-else>
<div id="search-map" v-if="itemCollection">
<Map :stac="stac" :stacLayerData="itemCollection" scrollWheelZoom popover />
</div>
<b-col class="right">
<div id="search-map" v-if="itemCollection">
<Map :stac="stac" :stacLayerData="itemCollection" scrollWheelZoom popover />
</div>
</b-col>
<Catalogs
v-if="isCollectionSearch" :catalogs="results" collectionsOnly
:pagination="pagination" :loading="loading" @paginate="loadResults"
Expand Down Expand Up @@ -102,7 +106,7 @@ export default {
itemFilters: {},
collectionFilters: {},
activeSearch: 0,
selectedCollections: {}
selectedCollections: {},
};
},
computed: {
Expand Down Expand Up @@ -242,7 +246,6 @@ export default {
this.loading = true;
try {
this.link = Utils.addFiltersToLink(link, this.filters, this.itemsPerPage);

let key = this.isCollectionSearch ? 'collections' : 'features';
let response = await stacRequest(this.$store, this.link);
if (response) {
Expand Down