Skip to content

Commit

Permalink
Merge pull request #2 from NiekBongers/main
Browse files Browse the repository at this point in the history
Typos, formatting, bash line
  • Loading branch information
GreatEmerald authored Sep 5, 2024
2 parents 396b9ef + 611399d commit 025a0b3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
38 changes: 19 additions & 19 deletions index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ dependencies:
The first line defines what the evnironment will be called (`geotest` in this example) and what packages should be installed (python, numpy and spyder). As you can see, this definition of the `geotest` environment is the exact same as the geotest environment as defined before. To create the geotest environment from such a file, save this yaml to a new file named `env.yaml`, or however you want to call it and use the argument `--file` (or `--f` in short):

```{r, eval=FALSE, engine='bash'}
mamba creat env -f env.yml
mamba env create -f env.yml
```


Expand Down Expand Up @@ -233,15 +233,15 @@ python test.py
The output is printed to the terminal. Running a script from the terminal is less error-prone than running it from an IDE (see the next section), such as Spyder, as IDEs often keep variables in memory after the script has finished running. Therefore, running a script from the terminal is a good final test before submitting an exercise or assignment.

## Python editors and IDEs
There are many Integrated Development Environments [IDE] for Python, and every programmer has their own preference. An IDE is a software application that provides facilities for software development.
There are many Integrated Development Environments (IDEs) for Python, and every programmer has their own preference. An IDE is a software application that provides facilities for software development.

* [Spyder](https://www.spyder-ide.org/) is a lightweight IDE. *In this course, Spyder is the recommended Python IDE.*
* [Jupyter notebooks](http://jupyter.org/) integrates visualization with code and is suitable to make tutorials, simple dashboards, quick visualizations, and do prototype testing. Jupyter Notebooks run in your browser on a localhost server or on a web server. They allow for various programming languages, e.g. Python, R, Julia, Spark or PySpark.
* [Jupyter notebook](http://jupyter.org/) integrates visualization with code and is suitable to make tutorials, simple dashboards, quick visualizations, and do prototype testing. Jupyter Notebook run in your browser on a localhost server or on a web server. They allow for various programming languages, e.g. Python, R, Julia, Spark or PySpark.
* [PyCharm Community Edition](https://www.jetbrains.com/help/pycharm/install-and-set-up-pycharm.html) is a free professional Python IDE with a lot of advanced functionality, such as integrated GIT version control, code completion, code checking, debugging and navigation. This IDE can optionally be used by more advanced scripters during this course instead of Spyder, but do know that you will not be assisted for solving IDE-related issues.

### Spyder

Spyder is a IDE for developing python mainly for scientific purposes. Fun fact, it is [completely written in python](https://github.com/spyder-ide/spyder)! Spyder is a very complete IDE that looks a bit like Rstudio. It shows the variables present in the current session, it has a code editor, a console and a figures pain in the main view.
Spyder is a IDE for developing python mainly for scientific purposes. Fun fact, it is [completely written in python](https://github.com/spyder-ide/spyder)! Spyder is a very complete IDE that looks a bit like Rstudio. It shows the variables present in the current session, it has a code editor, a console and a figures pane in the main view.

The [Spyder IDE](https://docs.spyder-ide.org/) can be started in a terminal when the *Spyder* package is installed in the active conda environment. So, using *Mamba*, make an environment and install Spyder to that environment. Activate the environment. Spyder will automatically make use of the Python interpreter of the active conda environment. To start Spyder:

Expand All @@ -260,17 +260,17 @@ In Spyder you should see an editor, a file explorer and a console. Have a look a
Open a new file and save it somewhere as `main.py` (File -- > New File --> Save As). Test writing a few lines of code and running the script.


### Jupyter Notebooks
### Jupyter Notebook

Jupyter Notebooks is actually not a IDE but it is very useful for writing code. Jupyter stands for the languages that once can use (*JU*lia, *PY*thon and *R*) and notebooks means that they are actually files instead of an IDE (such as Rstudio or Spyder). The notebooks can be interpreted and run by varying interpreters of which we will cover 2 later on. Jupyter Notebooks integrate code and visualization, and are therefore very helpful for demonstration purposes and to be run by online interperters (such as google colab). first we will show how to run Jupyter Notebooks locally. To do thi install `jupyter` and the module `folium` in an existing or new environment that includes Python and start Jupyter:
Jupyter Notebook is actually not a IDE but it is very useful for writing code. Jupyter stands for the languages that once can use (*JU*lia, *PY*thon and *R*) and notebooks means that they are actually files instead of an IDE (such as Rstudio or Spyder). The notebooks can be interpreted and run by varying interpreters of which we will cover two later on. Jupyter Notebook integrates code and visualization, and are therefore very helpful for demonstration purposes and to be run by online interpreters (such as Google Colab). First we will show how to run Jupyter Notebook locally. To do this install `jupyter` and the module `folium` in an existing or new environment that includes Python. To start Jupyter type:

```{r, eval=FALSE, engine='bash'}
jupyter notebook
```

Jupyter should pop up in your browser. Note that although jupyter is opened in your browser, internet is not used, the code is interpreted and run locally. You will see a menu with all files in your working directory. The Jupyter Notebook will only see files that are accessible from the working directory in which you launched the notebook!

Make a new folder: *New**Folder*, rename the folder (check the box next to the new 'Untitled Folder' and click **'Rename'** in the top) and, in this folder, create a new Python3 Jupyter Notebook *New**Python 3*. Give your notebook a name by clicking on *untitled*. Note that this creates a file with the extension *.ipynb*, which stands for Jupyter Notebook.
Make a new folder: *New**Folder*, rename the folder (check the box next to the new 'Untitled Folder' and click **'Rename'** in the top) and, in this folder, create a new Python3 Jupyter Notebook *New**Python 3*. Give your notebook a name by clicking on *untitled*. Note that this creates a file with the extension *.ipynb*, short for 'Interactive Python Notebook', which is the file format of Jupyter Notebook.

Feel free to have a go at the user interface tour (*Help**User Interface Tour*), or hover over the toolbar to check out the tools. The main tools are:

Expand All @@ -294,27 +294,27 @@ Run the code cell by selecting it and pressing the *Run* button, or press *CTRL

<img src="images/WUR_Basic_Folium_Map.png" alt="Wageningen University Basic Folium map" width="100%"></img>

Your Jupyter Notebook is automatically saved as an `.ipynb` file on your computer (the file extension comes from the historic name "IPython Notebook"). The notebook can be downloaded as a Python script, pdf or html. You can also save it manually.
Your Jupyter Notebook is automatically saved as an `.ipynb` file on your computer. The notebook can be downloaded as a Python script, pdf or html. You can also save it manually.

To exit a notebook properly, use *File**Close and Halt*. After that, by pressing Ctrl + c in the terminal where Jupyter Notebook server is running, you cancel the running process. The terminal goes back to command line and you can exit the virtual environment by typing `conda deactivate`.

```{r, engine='bash', eval=FALSE}
conda deactivate
```
### Google Colab
As said before, jupyter is locally opened in your browser. It does not connect to the internet, but it does show the possibilities, one could create something online that can run your notebooks for you on the cloud. This is exactly what Google does with Google Colab. Google Colab is a cloud service that allows you to run your jupyter notebooks on the Google cloud for free. Let's see what this looks like:
As said before, Jupyter is locally opened in your browser. It does not connect to the internet, but it does show the possibilities, one could create something online that can run your notebooks for you on the cloud. This is exactly what Google does with Google Colab. Google Colab is a cloud service that allows you to run your Jupyter notebooks on the Google cloud for free. Let's see what this looks like:

* Go to https://colab.research.google.com/notebooks/empty.ipynb (note the similaritie and differences between jupyter locally and on google colab)
* type `!pip install folium` and press ctrl+enter to run and install folium
* In a new cell run the same python code as locally to create and show a new folium map
* Go to https://colab.research.google.com/notebooks/empty.ipynb (note the similaritie and differences between Jupyter locally and on Google Colab);
* Type `!pip install folium` and press ctrl+enter to run and install folium;
* In a new cell run the same python code as locally to create and show a new folium map.

For this course we will rarely use jupyter notebooks and or google colab, but it is good to know they exist. Especially google colab is being used more and more in the scientific community and you are likely to come across these during other courses.
For this course we will rarely use Jupyter Notebook and or Google Colab, but it is good to know they exist. Especially Google Colab is being used more and more in the scientific community and you are likely to come across these during other courses.

# Putting it to the test

## Setting up the environment

Now that we know how to set up an environment and run code, lets use this new knowledge and run some python code. Again, During this course advise you to code in Spyder, as this IDE is the recommended IDE for the python part of this course. To practice you might also want to try out jupyter locally and google colab to run the same code.
Now that we know how to set up an environment and run code, lets use this new knowledge and run some Python code. Again, During this course advise you to code in Spyder, as this IDE is the recommended IDE for the Python part of this course. To practice you might also want to try out Jupyter locally and Google Colab to run the same code.

First, make a directory structure for this tutorial:

Expand Down Expand Up @@ -356,12 +356,12 @@ Important to note: for compatibility, it is best to install packages from the sa


## Quick refresher
In the tutorial about R and Python we have gone over the differences and similarities of python and R. This tutorial also contains some basic python syntax, in this tutorial we assume you know this content, but we will go over a few basics here as well. The example below are mostly meant for reference purposes, we assume you understand most of this refresher already.
In the tutorial about R and Python we have gone over the differences and similarities of python and R. This tutorial also contains some basic python syntax, in this tutorial we assume you know this content, but we will go over a few basics here as well. The examples below are mostly meant for reference purposes, we assume you understand most of this refresher already.

### Printing and basic data types
In python we assign variable using the equals sign (`=`):
In Python we assign variable using the equals sign (`=`):

Printing in python is done using the `print` function. We can print variables directly:
Printing in Python is done using the `print` function. We can print variables directly:

```{r, engine = 'Python', eval=FALSE}
# Integer
Expand All @@ -380,7 +380,7 @@ is_student = True
print(name)
```

We can use string formatting to use flexible strings, for example for printing. to start a formatted string, we put a `f` before the string. We can use curly brackets `{}` in this formatted string. The text between these curly brackets is executed as regular python code.
We can use string formatting to use flexible strings, for example for printing. to start a formatted string, we put a `f` before the string. We can use curly brackets `{}` in this formatted string. The text between these curly brackets is executed as regular Python code.

```{r, engine = 'Python', eval=FALSE}
# String formatting and printing
Expand Down Expand Up @@ -482,7 +482,7 @@ del person["is_student"]
### Importing packages
Python is used by a very large community, as is said before. One of the reasons for this is that this entire community builds a lot of (open source) packages. It is therefor very useful to be able to build upon these packages. In R you have worked a with *dataframes* and *spatial dataframes*. In Python these are not standard datatypes, but they are implemented in very well known packages called `Pandas` and its spatial counterpart `GeoPandas.` We will go in much more detail during the Python-Vector tutorial but we will introduce them quickly here.

In python we import a package using the `import` statement (instead of th the `library` function in R) . For example importing the pandas package goes as follows
In Python we import a package using the `import` statement (instead of th the `library` function in R) . For example importing the pandas package goes as follows:

```{r, engine = 'Python', eval=FALSE}
import pandas as pd
Expand Down
86 changes: 43 additions & 43 deletions index.html

Large diffs are not rendered by default.

0 comments on commit 025a0b3

Please sign in to comment.