Installing Scripts Lesson

For Windows

After you've installed VSCode using WSL, install Anaconda on WSL.

Anaconda is like a big tool box for your computer, it has tools and libraries that you might need for data analysis, programming, and computing. Anaconda Python packages include pandas, numpy, sqlite, jupyter, bash, and other kernels. Conda is a tool that helps you manage these tools inside Anaconda. It lets you install, update, delete, and organize packages and materials. Conda script tells Conda what tools and materials you need and how to install them in your Anaconda toolbox.

conda image

Type these commands in powershell or terminal

PS C:\Users\UserName> wsl  # Windows prompt to WSL command
$ cd /tmp # used to store temporary files 
$ wget https://repo.anaconda.com/archive/Anaconda3-2023.03-Linux-x86_64.sh # downloadable file
$ chmod +x Anaconda3-2022.05-Linux-x86_64.sh # chmod (change mode command) changes permissions for a file or directory 
# Answer yes to all the prompts
$ ./Anaconda3-2022.05-Linux-x86_64.sh 

Start a new WSL Command or Powershell. Now the WSL prompt should be prefixed with (base) from Anaconda install. If not, go back to Anaconda install. The base prefix indicates that you are running inside the conda/anaconda environment. The command "conda deactivate" should bring you back.

PS C:\Users\ShayM> wsl  # Windows prompt
(base) shay@MSI:/mnt/c/Users/ShayM$ cd ~ # WSL prompt
(base) shay@MSI:~$ # WSL home, best place to install files

# you can check your conda versions
(base) id:~$ conda --version 

command prompt view

For MacOS

Python2 install on MacOS

  • Install Python2> VSCode install on MacOS.
  • Install VSCode> Anaconda install on MacOS.
  • Download for MacOS:Anaconda- Run Install: Answer yes to questions Homebrew install on MacOS
  • Homebrew is a tool that helps you easily install and manage software on your Mac. Think of it like a virtual store for your computer where you can browse, download and install a variety of useful programs and tools.
  • Copy and Paste to Install from Terminal Homebrew - Copy bash ... curl ... command using copy box on website
    • Launch terminal from search bar
    • Paste bash ... curl ... command into Terminal ...
    • Make sure command starts, this should provide feedback/output in terminal and could take a long time, like 10-min, there could be a prompt in the middle, at about 5-minutes. Follow any on screen instructions provided in terminal output to finish.
  • Homebrew installs a tool called "brew" which helps add and manage developer packages on MacOS.

Start a new WSL terminal. Now the terminal prompt should be prefixed with (base) from Anaconda install. If not, go back to Anaconda install.

Having Homebrew and Anaconda allows you to easily install Key Packages needed on MacOS like the commands below are all neccessary:```bash$ brew list # list packages $ brew update # update package list $ brew upgrade # upgrade packages $ brew install git # install latest git $ brew install python # install python3 for development $ python --version # version of python3 installed $ brew install java # openjdk install

  • Hacks: Screen shot that you have conda installed

Setting up GitHub Pages with FastPages Features

All of you have created your own repository before at the beginning of the year, but some things have changed in the process.

  1. Duplicate APCSP, and use it as template for your own repo

  1. Name your repo and include all branches before clicking the green create button.

  1. If you have an automatically generated pull request, then go ahead and follow the instructions on there. If there is no pull request, as shown below, then move on to step 4.

  1. Navigate to this link. Select: RSA and 4096 and leave Passphrase blank. Click the blue button Generate-SSH-Keys. You have just created an ssh key-pair.

  1. Navigate to this link. Replace the section of the link "yourrepositoryname" with your repository name. In the picture, I have inputted “ourshiny,” because it is my repository name. Replace the section of the link "yourgithubname" with your repository name that you just made. https://github.com/yourgithubname/yourrepositoryname/settings/secrets/actions

  1. Click New repository secret. Copy and paste the Private Key (generated in step 4) into the Value field. This includes the "---BEGIN RSA PRIVATE KEY---" and "--END RSA PRIVATE KEY---" portions. In the Name field, name the secret SSH_DEPLOY_KEY. Once done, click the green Add Secret button.

  1. Navigate to this link, then replace the portions below with your github and repository names. My github name in the image below is jiya-sav, and my repository name is ourshiny. https://github.com/yourgithubname/yourrepositoryname/settings/keys

  1. Click the Add deploy key button. Paste your Public Key (generated in step 4) into the Key box. In the Title, name the key anything you want, for example fastpages-key. Finally, make sure you click the checkbox next to Allow write access (pictured below), and click the green Add key button to save the key.

  1. GitHub Actions will build your site, which will take 2-3 minutes to complete. This will happen anytime you push changes to the master branch of your repository. You can monitor the logs of this if you like on the Actions tab of your repo. Your GH-Pages Status badge on your README will eventually appear and be green, indicating your first successful build. You can monitor the status of your site in the GitHub Pages section of your repository settings.

Now that you have made your repository secure, you can clone it into vscode and CODE, CODE, CODE!!!

How to Make a Pull Request

  1. After you've cloned a repository and made changes to it, go to pull requests in your Github repository pull request 1

  2. Once you click 'create new pull request,' you should see this with your new changes: pull request 2

  3. Change the title and send the pull request pull request 3

  • Hacks: You will be turning in all your hacks as a pull request. Title the pull request as shown in the third step but with your own name. Any other titles will not be accepted.

Fetching

The Basics

  • Fetching means getting data from somewhere else, like a website or database.

JavaScript is a programming language that can be used to fetch data from a website or database.

  • When you fetch data with JavaScript, you first need to tell it where to get the data from. This is called the URL.
  • Once you know the URL, you can use JavaScript to go and get the data from that URL.
  • When the data comes back, it's usually in a format like text or JSON. This means that you might need to do some additional work to turn the data into something that your website or application can use.
  • Once you have the data, you can use JavaScript to display it on your website or application. You can put it in a table, show it on a map, or do anything else you can think of.

About Fetching

Method

The fetching methods are in line with the CRUD (Create, Read, Update, Delete), and list as follows:

  • "GET": Probably the most common method you would use, which is in line with "read". It essentially "gets" the information from the API and brings it to the frontend where you are able to display it and read it.
  • "POST": Lines up with "create" and adds a resource to the API. You will probably mostly be using this with your own API or if a website adds your input to an API.
  • "PUT" and "PATCH": Essentially the "update" for API. Use "PUT" if you want to change the whole resource, or "PATCH" if you want to change just one part of a resource.
  • "DELETE": Exactly how it sounds, deletes a resource from the API

Popcorn Hacks:

  • What method should I use if I want to display some of the data stored in the backend?

GET

  • What method would I use if something in the data is misspelled and I want to fix it real quick?

PUT

  • What method should I use if I want to add a new entry to the data?

POST

Filtering Data

  • If we want to display API data, we are going to have to filter what we want to display for multiple reasons
    • These include making sure that the user only sees information they need to see, and not anything else
    • Also we need to present the information in a readable format instead of just seeing the raw data

The below code segment, is from a previous project. It shows the use of a url being fetched, and the JavaScript fetch() command being implemented.

// prepare fetch urls
  // const url = "https://ssjn.nighthawkcodescrums.gq/api/song";
  const url = "https://ssjncpt.duckdns.org/api/song";
  const sad_url = url + "/sad/";  // sad category
  const happy_url = url + "/happy/";  // happy category
  const rage_url = url + "/rage/";  // rage category
  const indian_url = url + "/indian/"; // indian category

  // prepare fetch GET options
  const options = {
    method: 'GET', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // cors, *cors, same-origin
    cache: 'default', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'omit', // include, *same-origin, omit
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
  };
  // prepare fetch PUT options, clones with JS Spread Operator (...)
  const put_options = {...options, method: 'PUT'}; // clones and replaces method

  // fetch the API
  fetch(url, options)
    // response is a RESTful "promise" on any successful fetch
    .then(response => {
      // check for response errors
      if (response.status !== 200) {
          error('GET API response failure: ' + response.status);
          return;
      }
      // valid response will have JSON data

Third Party APIs

  • APIs provided by other developers for public use.

  • Programmers can use the content of these APIs to create new functionalities for their programs, or they can also simply take advantage of the data of the API for different uses.

    • Ex: A developed can use Google Account login to allow users to log in with their google account

    • Ex: Using the content of a weather API to make a weather app

  • The functionalities of these APIs are usually accessed via JavaScript (usually) as you’ve seen before in your past projects.

  • 3rd party APIs are located on third party servers. To access them from JavaScript you first need to connect to the API functionality and make it available on your page.

<script src="https://api.mqcdn.com/sdk/mapquest-js/v1.3.2/mapquest.js" defer></script>
<link  rel="stylesheet"  href="https://api.mqcdn.com/sdk/mapquest-js/v1.3.2/mapquest.css" />
  • Here is an example of accessing the objects available in the library above.
const map = L.mapquest.map('map', {
    center: [53.480759, -2.242631],
    layers: L.mapquest.tileLayer('map'),
    zoom: 12
  });
  • The process of accessing and using third-party APIs can vary depending on the specific API and the requirements set by the API provider. 3rd party APIs use developer keys to allow developers access to the API functionality, which protects the owner of the API.
L.mapquest.key = 'YOUR-API-KEY-HERE';
  • Developers must apply to get the key of a certain API, and then include it in their code to be allowed access to the API's functionality. Some API keys are accessed slightly differently, but the method is similar for the most part.

Put the link of a 3rd party API you have or will use below?

Weather App Project: https://github.com/dolphinalt/Weather-App

API url: https://visual-crossing-weather.p.rapidapi.com/forecast

Pandas

While 3rd party APIs give you access to a plethora of data you may have not been able to include using a completely new self-made database, pandas can help you organize and utlize this data.

Pandas is an open-source library that allows for the manipulation and analysis of numerical data, which is vital in real-life applications.

We have used this in the song recommendation program we are currently developing. The use of pandas allows us to take the csv file we obtained from a third party and display it in a format we can manipulate and analyze.

import os
import pandas as pd
from flask import render_template

@app.route('/songdata')
def songdata():
    # Path to the CSV file
    csv_path = os.path.join(app.static_folder, 'songdata.csv')

    # Read the CSV file into a pandas dataframe
    df = pd.read_csv(csv_path)

    # Render the dataframe as an HTML table using Jinja2
    return render_template('songdata.html', table=df.to_html(index=False))

This is how the dataframe will appear on our flask:

Python (Request) Fetch

Example with Weather API

  • What method is being used in these cells?
import requests

url = "https://yahoo-weather5.p.rapidapi.com/weather"

querystring = {"location":"San Diego","format":"json","u":"f"}

headers = {
	"X-RapidAPI-Key": "4abcb54450msh7468dfd72294e89p18fbaajsn6d4200063b39",
	"X-RapidAPI-Host": "yahoo-weather5.p.rapidapi.com"
}

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.json())
{'location': {'city': 'San Diego', 'woeid': 2487889, 'country': 'United States', 'lat': 32.831699, 'long': -117.122002, 'timezone_id': 'America/Los_Angeles'}, 'current_observation': {'pubDate': 1682463385, 'wind': {'chill': 64, 'direction': 'WSW', 'speed': 9}, 'atmosphere': {'humidity': 65, 'visibility': 10, 'pressure': 1012.5}, 'astronomy': {'sunrise': '6:08 AM', 'sunset': '7:26 PM'}, 'condition': {'temperature': 65, 'text': 'Mostly Cloudy', 'code': 28}}, 'forecasts': [{'day': 'Tue', 'date': 1682438400, 'high': 67, 'low': 55, 'text': 'Mostly Clear', 'code': 33}, {'day': 'Wed', 'date': 1682524800, 'high': 73, 'low': 56, 'text': 'Partly Cloudy', 'code': 30}, {'day': 'Thu', 'date': 1682611200, 'high': 76, 'low': 57, 'text': 'Mostly Sunny', 'code': 34}, {'day': 'Fri', 'date': 1682697600, 'high': 76, 'low': 58, 'text': 'Mostly Sunny', 'code': 34}, {'day': 'Sat', 'date': 1682784000, 'high': 77, 'low': 59, 'text': 'Partly Cloudy', 'code': 30}, {'day': 'Sun', 'date': 1682870400, 'high': 75, 'low': 59, 'text': 'Partly Cloudy', 'code': 30}, {'day': 'Mon', 'date': 1682956800, 'high': 64, 'low': 54, 'text': 'Cloudy', 'code': 26}, {'day': 'Tue', 'date': 1683043200, 'high': 62, 'low': 52, 'text': 'Mostly Cloudy', 'code': 28}, {'day': 'Wed', 'date': 1683129600, 'high': 60, 'low': 53, 'text': 'Partly Cloudy', 'code': 30}, {'day': 'Thu', 'date': 1683216000, 'high': 62, 'low': 51, 'text': 'Partly Cloudy', 'code': 30}, {'day': 'Fri', 'date': 1683302400, 'high': 63, 'low': 53, 'text': 'Mostly Cloudy', 'code': 28}]}

The fetch worked and we can see all the data, but this is presented in a way that is not very readable. It also has a lot of information we didn't see. Let's say I just want to see the projected weather forecasts.

import requests

url = "https://yahoo-weather5.p.rapidapi.com/weather"

querystring = {"location":"San Diego","format":"json","u":"f"}

headers = {
	"X-RapidAPI-Key": "4abcb54450msh7468dfd72294e89p18fbaajsn6d4200063b39",
	"X-RapidAPI-Host": "yahoo-weather5.p.rapidapi.com"
}

response = requests.request("GET", url, headers=headers, params=querystring)

print("San Diego Weather Forecast:" + "\n")
forecast = response.json().get("forecasts")
i = 0
while i < len(forecast):
	for key, value in forecast[i].items():
		print("\t" + key, value)
	i += 1
	print("\n")
San Diego Weather Forecast:

	day Tue
	date 1682438400
	high 67
	low 55
	text Mostly Clear
	code 33


	day Wed
	date 1682524800
	high 73
	low 56
	text Partly Cloudy
	code 30


	day Thu
	date 1682611200
	high 76
	low 57
	text Mostly Sunny
	code 34


	day Fri
	date 1682697600
	high 76
	low 58
	text Mostly Sunny
	code 34


	day Sat
	date 1682784000
	high 77
	low 59
	text Partly Cloudy
	code 30


	day Sun
	date 1682870400
	high 75
	low 59
	text Partly Cloudy
	code 30


	day Mon
	date 1682956800
	high 64
	low 54
	text Cloudy
	code 26


	day Tue
	date 1683043200
	high 62
	low 52
	text Mostly Cloudy
	code 28


	day Wed
	date 1683129600
	high 60
	low 53
	text Partly Cloudy
	code 30


	day Thu
	date 1683216000
	high 62
	low 51
	text Partly Cloudy
	code 30


	day Fri
	date 1683302400
	high 63
	low 53
	text Mostly Cloudy
	code 28


As we can see, we now can see the data in a much more organized and readable format, and we only see the forecasts, which is what we wanted to see

Hacks and Point Division

  • Popcorn questions answered within lesson (.2)
  • Screenshot showing proof that conda is installed (.1)
  • Create 3 quiz questions to demonstrate the main steps in creating a repository on Github (.1)
  • Fetch from a third party API, and display json data. Then use the json data in any simple JavaScript function (hint: parse, this makes json data into a JavaScript object) (.4)
    • In your code, comment on how you implemented a 3rd party API. Reflect on how using the 3rd party API helped.
  • Pull request successful completion: (.1)

Extra: Use fetch() and display data from an API, self-made or third party (.1)

Hacks

Screenshot showing proof that conda is installed (.1)

CondaInstalled

Create 3 quiz eustions to demonstrate the main steps in creating a repository on github (.1)

1. What is the best way to get a copy of an existing github repository?

(A) Fork the repository
(B) Clone the repository
(C) Create a new repository and copy the files over
(D) Create a pull request

The correct answer is (B)


2. If I wanted to make a change to a friend's CSP project, what would I use to ask if they want to accept my changes?

(A) Fork the repository
(B) Clone the repository
(C) Create an issue in his repository
(D) Create a pull request

The correct answer is (D)


3. Which of the following best describes forking a repository?

(A) Making a branch of the repository in order to make your own changes separate too the main working branch.
(B) Creating a copy of the repository.
(C) Letting people know that there is an issue with the current code and asking for a fix.
(D) None of the above

The correct answer is (A)

Fetch from a third party API, and display json data. Then use the json data in any simple JavaScript function (hint: parse, this makes json data into a JavaScript object) (.4)

import http.client

conn = http.client.HTTPSConnection("visual-crossing-weather.p.rapidapi.com")

headers = {
    'content-type': "application/octet-stream",
    'X-RapidAPI-Key': "68b28d2802msh609f62c1192337dp1eca9ejsnb9175b996d5a",
    'X-RapidAPI-Host': "visual-crossing-weather.p.rapidapi.com"
}

conn.request("GET", "/forecast?aggregateHours=24&location=Washington%2CDC%2CUSA&contentType=csv&unitGroup=us&shortColumnNames=0", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8")) # the .decode function turns the json object into a printable string, similar to the .stringify json function in javascript

# This api helped me because it allows me to quickly and easily get weather conditions to use within my project, instead of needing to go and manually input the data myself.
Address,Date time,Latitude,Longitude,Resolved Address,Name,Wind Direction,Minimum Temperature,Maximum Temperature,Temperature,Wind Speed,Cloud Cover,Heat Index,Chance Precipitation (%),Precipitation,Sea Level Pressure,Snow Depth,Snow,Relative Humidity,Wind Gust,Wind Chill,Conditions
"Washington,DC,USA","04/28/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",70.0,58.0,58.0,58.0,11.4,100.0,,73.0,0.09,1010.0,0.0,0.0,93.7,16.1,,"Rain, Overcast"
"Washington,DC,USA","04/29/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",119.2,56.9,67.0,61.0,6.5,87.0,,73.0,0.52,1008.4,0.0,0.0,85.6,13.9,,"Rain, Overcast"
"Washington,DC,USA","04/30/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",138.3,56.9,65.0,60.7,8.7,88.9,,98.0,0.88,997.2,0.0,0.0,88.7,13.9,,"Rain, Overcast"
"Washington,DC,USA","05/01/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",252.9,49.9,61.1,56.3,15.1,33.0,,16.0,0.03,994.0,0.0,0.0,58.9,31.1,46.6,"Partially cloudy"
"Washington,DC,USA","05/02/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",251.3,50.1,58.4,54.3,16.5,58.7,,43.0,0.0,1001.1,0.0,0.0,56.7,29.3,42.9,"Partially cloudy"
"Washington,DC,USA","05/03/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",291.3,46.8,58.9,53.0,16.8,36.3,,33.0,0.0,1004.8,0.0,0.0,56.6,31.1,40.9,"Partially cloudy"
"Washington,DC,USA","05/04/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",309.2,47.2,64.8,56.2,12.9,31.2,,36.0,0.0,1013.0,0.0,0.0,57.9,22.8,41.8,"Partially cloudy"
"Washington,DC,USA","05/05/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",215.8,51.7,65.4,58.9,8.1,52.6,,40.0,0.04,1019.7,0.0,0.0,66.1,15.2,42.2,"Partially cloudy"
"Washington,DC,USA","05/06/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",248.0,44.9,63.2,54.3,5.0,28.3,,42.9,0.0,1022.8,0.0,0.0,65.4,12.4,43.1,"Partially cloudy"
"Washington,DC,USA","05/07/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",314.4,48.6,72.4,60.0,6.4,30.2,,33.3,0.0,1021.7,0.0,0.0,59.0,18.8,46.0,"Partially cloudy"
"Washington,DC,USA","05/08/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",264.4,53.0,75.1,64.5,4.1,12.9,,19.0,0.0,1020.5,0.0,0.0,54.9,10.1,,"Clear"
"Washington,DC,USA","05/09/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",211.9,56.4,81.2,69.2,7.0,95.8,80.2,38.1,0.0,1016.3,0.0,0.0,52.4,32.7,,"Overcast"
"Washington,DC,USA","05/10/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",241.9,66.8,82.3,73.1,8.8,39.1,81.1,47.6,0.01,1014.4,0.0,0.0,53.1,31.3,,"Partially cloudy"
"Washington,DC,USA","05/11/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",213.8,64.7,89.9,76.4,9.8,34.2,88.3,19.0,0.01,1013.3,0.0,0.0,58.1,26.8,,"Partially cloudy"
"Washington,DC,USA","05/12/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",225.4,67.2,73.8,70.8,6.6,99.6,,28.6,0.1,1015.6,0.0,0.0,79.8,24.4,,"Overcast"
"Washington,DC,USA","05/13/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",116.8,64.8,71.0,67.0,4.3,99.9,,42.9,0.1,1020.2,0.0,0.0,83.7,19.8,,"Overcast"
"Washington,DC,USA","05/14/2023",38.8904,-77.032,"Washington, DC, United States","Washington,DC,USA",49.8,63.8,64.7,64.0,5.0,100.0,,14.3,0.4,1021.3,0.0,0.0,97.4,16.6,,"Overcast"