Developer Blog

Tipps und Tricks für Entwickler und IT-Interessierte

Ionic | App from Scratch – Two Sidemenus

Prepare your starter App

ionic start App sidemenu --type angular --no-git

Change into the newly create folder and start the Ionic App

cd App
ionic serve

You will see the default Ionic Sidemenu App

The entries of the sSidemenu are defined in app.components.ts:

export class AppComponent {
	public appPages = [
		{ title: 'Home', url: '/home', icon: 'home' },
		{ title: 'List', url: '/list', icon: 'list' }
	];

The sidemenu itself and his apperance are defined in app.component.html:

<ion-app>
  <ion-split-pane contentId="main-content">
    <ion-menu contentId="main-content" type="overlay">
      <ion-header>
        <ion-toolbar>
          <ion-title>Menu</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content>
        <ion-list>
          <ion-menu-toggle auto-hide="false" *ngFor="let p of appPages">
            <ion-item [routerDirection]="'root'" [routerLink]="[p.url]">
              <ion-icon slot="start" [name]="p.icon"></ion-icon>
              <ion-label>{{p.title}}</ion-label>
            </ion-item>
          </ion-menu-toggle>
        </ion-list>
      </ion-content>
    </ion-menu>
    <ion-router-outlet id="main-content"></ion-router-outlet>
  </ion-split-pane>
</ion-app>

The Sidemenu entries and the Icon are added in line 13 and 14 to the sidemenu.

Define two sidemenus in app.html

<ion-menu [content]="contentLeft"> ....</ion-menu>
<ion-menu side="right" [content]="contentRight">....</ion-menu>
 ....

<ion-nav [root]="rootPage" #contentLeft #contentRight swipeBackEnabled="false"></ion-nav>

Add buttons to page

<ion-header>
   <ion-navbar>
      <button ion-button start menuToggle>
         <ion-icon name="menu"></ion-icon>
      </button>
      <ion-title>Demo of two Sidemenus</ion-title>
      <button ion-button end menuToggle="right">
         <ion-iconname="menu"></ion-icon>
      </button>
   </ion-navbar>
</ion-header>
<ion-content padding>
</ion-content>
Result
Github Repository

You can find the code here

Ionic | App from Scratch

Prepare Environment

Install Node.JS

$ LATEST=18.16.0 
$ nvm install $LATEST --latest-npm node --version  
$ npm --version 
$ nvm alias latest $LATEST
$ nvm alias default $LATEST 

Install Ionic

$ npm -g install ionic@latest
$ ionic -version

Create an app from Template

ionic start --list
ionic start sidemenu sidemenu
ionic start super super

Introduction: First App

Prepare the base Application

Create basic structure from template

$ ionic start app_from_scratch sidemenu --type angular
$ cd app_from_scratch

Some name changing

To keep the structure of the filenames the same, i like to change the name for the routing module (changing the dash to a dot)

from

app-routing.module.ts

to

app-routing.module.ts

Show base app

$ ionic serve -c
bildschirmfoto-2016-11-16-um-18-04-31

Add a page

Create a new page with the name “About”

$ ionic generate page About

Add import to app.components.ts

  public appPages = [
    { title: 'Home', url: '/home', icon: 'home' },
    { title: 'List', url: '/list', icon: 'list' },
    { title: 'About', url: '/about', icon: '' }
  ];

Add item to app-routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadChildren: './home/home.module#HomePageModule' },
  { path: 'list', loadChildren: './list/list.module#ListPageModule' },
  { path: 'about', loadChildren: './about/about.module#AboutPageModule' }
];

Save the file now, and you will see the sidemenu entry:

Looks great, but… where is the sidemenu?

Ok, if you check the home page html code (home.page.html), you will find a button for the side menu

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>Home</ion-title>
  </ion-toolbar>
</ion-header>

Missing in about.page.html:

<ion-header>
  <ion-toolbar>
    <ion-title>About</ion-title>
  </ion-toolbar>
</ion-header>

So, we had to add the desired code snippet

<ion-header>
    <ion-toolbar>
      <ion-buttons slot="start">
        <ion-menu-button></ion-menu-button>
      </ion-buttons>
      <ion-title>About</ion-title>
    </ion-toolbar>
  </ion-header>

Add some Ionic Components

Look at the Ionic Preview App, or read the documentation, if you what to dive in immediately.

Or just copy some code from the documentation page and try by yourself:

bildschirmfoto-2016-11-16-um-19-01-22
bildschirmfoto-2016-11-16-um-18-58-48

We describe the steps to add additional components in detail in this blog.

Directory structure of an application

One possible way to build an app is:

  • create a base app from blank template
  • add desired pages, code and components

To find out the necessary steps to add an new page or component, look into the templates and compare a template with the blank template. the differences are the required steps to an a component.

For example, if you want to add tabs to your app, then compare the blank template with the tabs template:

$ ionic start starter_tabs  tabs  --type angular --no-deps --no-git --no-link
$ ionic start starter_blank blank --type angular --no-deps --no-git --no-link

The diff command shows the changes between the two templates:

$ diff -r -w starter_tabs starter_blank
diff -r -w starter_blank/ionic.config.json starter_tabs/ionic.config.json
2c2
<   "name": "starter_blank",
---
>   "name": "starter_tabs",
diff -r -w starter_blank/package.json starter_tabs/package.json
2c2
<   "name": "starter_blank",
---
>   "name": "starter_tabs",
Only in starter_tabs/src/app: about
diff -r -w starter_blank/src/app/app-routing.module.ts starter_tabs/src/app/app-routing.module.ts
5,6c5
<   { path: '', redirectTo: 'home', pathMatch: 'full' },
<   { path: 'home', loadChildren: './home/home.module#HomePageModule' },
---
>   { path: '', loadChildren: './tabs/tabs.module#TabsPageModule' }
8d6
< 
diff -r -w starter_blank/src/app/app.module.ts starter_tabs/src/app/app.module.ts
3c3
< import { RouterModule, RouteReuseStrategy, Routes } from '@angular/router';
---
> import { RouterModule, RouteReuseStrategy } from '@angular/router';
9d8
< import { AppComponent } from './app.component';
10a10
> import { AppComponent } from './app.component';
Only in starter_tabs/src/app: contact
diff -r -w starter_blank/src/app/home/home.module.ts starter_tabs/src/app/home/home.module.ts
0a1,2
> import { IonicModule } from '@ionic/angular';
> import { RouterModule } from '@angular/router';
3d4
< import { IonicModule } from '@ionic/angular';
5,6d5
< import { RouterModule } from '@angular/router';
< 
10a10
>     IonicModule,
13,19c13
<     IonicModule,
<     RouterModule.forChild([
<       {
<         path: '',
<         component: HomePage
<       }
<     ])
---
>     RouterModule.forChild([{ path: '', component: HomePage }])
diff -r -w starter_blank/src/app/home/home.page.html starter_tabs/src/app/home/home.page.html
3,5c3
<     <ion-title>
<       Ionic Blank
<     </ion-title>
---
>     <ion-title>Home</ion-title>
diff -r -w starter_blank/src/app/home/home.page.spec.ts starter_tabs/src/app/home/home.page.spec.ts
14,15c14
<     })
<       .compileComponents();
---
>     }).compileComponents();
diff -r -w starter_blank/src/app/home/home.page.ts starter_tabs/src/app/home/home.page.ts
6c6
<   styleUrls: ['home.page.scss'],
---
>   styleUrls: ['home.page.scss']
8,10c8
< export class HomePage {
< 
< }
---
> export class HomePage {}
Only in starter_tabs/src/app: tabs

Some differences come from the different names (starter_blank vs. starter_tabs, HomePage vs. TabsPage). The significant changes are those, which are new in the starter_tabs directory.

PostgreSQL | Getting started

Install

PostgreSQL Server

Mac OS

brew install postgres

Windows (Installer)

Windows (Zip-ARchiv)

Download postgresql from https://www.enterprisedb.com/download-postgresql-binaries

Configure

Initialize database:

initdb.exe -D ../data –-username=postgres –-auth=trust

Start the database:

pg_ctl.exe start -D ../data

Log into the database:

psql –-username=postgres

Create a password for your user:

ALTER USER postgres WITH PASSWORD ‘VeryVerySecret’;

Install adminpack extension:

CREATE EXTENSION adminpack;

Manage Users

Preparation

psql postgres

List all Users

postgres=# \du+

Create Superuser

 create role superuser superuser login password 'pAssw8rd';

Change Password

Postgres Utilities

Documentation

Working with

iPython | Getting Started

Introduction

iPython ist ein Kommandozeileninterpreter zum interaktiven Arbeiten mit der Programmiersprache Python

IDEs and Environments

Install 

Jupyter

$ virtualenv --python python3 jupyter
$ cd jupyter
$ . bin/activate
$ pip install --upgrade pip
$ pip install jupyter

Finally run jupyter

jupyter notebook

Anaconda

$ virtualenv --python python3 anaconda
$ cd anaconda
$ . bin/activate
$ wget https://repo.continuum.io/archive/Anaconda3-4.3.1-Linux-x86_64.sh | bash
jupyter notebook

Resources

Awesome Notebooks

Notebooks

Ubuntu | Cookbook

User and Groups

Change users name

exec sudo -i
killall -u [oldname]
id [oldname]
usermod -l [newname] [oldname]
groupmod -n [newname] [oldname]
usermod -d /home/[newname] -m [newname]
usermod -c "[full name (new)]" [newname]
id [newname]

Change Runlevel

The systemctl isolate is temporary way. To make it permanent you have to use systemctl set-default as shown below:

sudo systemctl set-default multi-user.target

and reboot.

To revert graphical session use

sudo systemctl set-default graphical.target

Package management

apt-get updaterefresh available updates
apt-get upgradeupgrade with package replacements; 
apt-get dist-upgradeupgrade Ubuntu version
apt-get install pkginstall pkg
apt-get purge pkguninstall pkg
apt-get autoremoveRemove obsolete packages
apt-get -f install
try to fix broken packages
dpkg -i pkg.debinstall file pkg.deb (file)
dpkg --configure -a
try to fix broken packages

Info about the system

ls_release -a
 Find out used version

Python | Arbeiten mir virtuellen Umgebungen

Allgemein

Virtuelle Umgebung stellen eigenständige Umgebungen dar, in denen Programme und Pakete installiert/modifiziert werden können ohne die Betriebssystemumgebung zu verändern.

Dadurch können zum Beispiel andere Versionen von Python oder von Python-Modulen getestet werden.

Technisch gesehen ist eine virtuelle Umgebung ein Verzeichnis, in dem die gewünschten Pakete und Programme bzw. die gewünschte Python-Version installiert ist. Entsprechend werden die notwendigen Umgebungsvariablen modifiziert: PATH, PYTHONLIB, …

Arbeiten mit einer virtuellen Umgebung

  • Virtuelle Umgebung einrichten/installieren (einmalig)
  • Virtuelle Umgebung aktivieren
  • “Arbeiten” in der Umgebung, z. B ein Python-Programm ausführen
  • Virtuelle Umgebung deaktivieren

Einrichten einer virtuellen Umgebung

Zur Unterstützung von virtuellen Umgebungen gibt es mehrere Programme/Pakete

  • venv
  • poetry
  • pyenv
  • virtualenv

venv

Installation

Einrichten einer virtuellen Umgebung

➜ python -m venv .venv


➜ pip -V
pip 21.2.4 from D:\python\venv\lib\site-packages\pip (python 3.9)

➜ & .\.venv\Scripts\Activate.ps1

➜ pip -V
pip 21.1.3 from d:\venv\.venv\lib\site-packages\pip (python 3.9)

poetry

Installation

$ pip install poetry

Einrichten einer virtuellen Umgebung

$ poetry new app
Created package app in app
$ cd app
$ tree .
.
├── README.rst
├── app
│   └── __init__.py
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_app.py

2 directories, 5 files
$ poetry config virtualenvs.create true  --local
$ poetry config virtualenvs.in-project true --local
$ tree .
.
├── README.rst
├── app
│   └── __init__.py
├── poetry.toml
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_app.py

2 directories, 6 files

Inhalt der Projektdatei poetry.toml

[virtualenvs]
create = true
in-project = true
➜ poetry install
Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 10 installs, 0 updates, 0 removals

  • Installing pyparsing (2.4.7)
  • Installing atomicwrites (1.4.0)
  • Installing attrs (21.2.0)
  • Installing colorama (0.4.4)
  • Installing more-itertools (8.8.0)
  • Installing packaging (21.0)
  • Installing pluggy (0.13.1)
  • Installing py (1.10.0)
  • Installing wcwidth (0.2.5)
  • Installing pytest (5.4.3)

Installing the current project: app (0.1.0)
$ tree .
.
├── README.rst
├── app
│   └── __init__.py
├── poetry.lock
├── poetry.toml
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_app.py

virtualenv

Installation

Installation des Paketes pyenv

$ brew install pyenv-virtualenv

Anpassen der Bash Startdatei .bashrc

 if which pyenv-virtualenv-init > /dev/null; then
     eval "$(pyenv virtualenv-init -)"
 fi

Virtuelle Umgebung einrichten

Auflisten aller zur Verfügung stehenden Python Versionsn

$ pyenv install --list

Installation einer Python Version

pyenv install 3.7.2

Auflisten aller installierten Versionen

$ pyenv versions
* system (set by /Users/<your username>/.pyenv/version)
  2.7.14
  3.7.1
  3.7.2
# Python 2
$ pyenv virtualenv 2.7.14 venv
# Python 3
$ pyenv virtualenv 3.7.2 venv

Umgebungen anzeigen: die letzen beiden Zeilen zeigen die soeben eingerichtete Umgebung an

$ pyenv versions
* system (set by /Users/RalphG/.pyenv/version)
  2.7.14
  3.7.1
  3.7.2
  3.7.2/envs/venv
  venv

Alternativ: nur die virtuellen Umgebungen anzeigen

$ pyenv virtualenvs

Umgebung aktivieren / deaktivieren

$ pyenv activate <name>
$ pyenv deactivate

Umgebung testen

$ which python
~/pkg.virtualenv/bin/python

Das Paket virtualenv

Installation

$ pip install virtualenv

Erstellen einer virtuellen Umgebung

Feststellen des Pfades der gewünschten Python Version

Python 2

$ which python2
/usr/local/bin/python2

Python 3

$ which python3
/usr/local/bin/python3

Einrichten der virtuellen Umgebung

Python 2

$ virtualenv venv2 --python /usr/local/bin/python2

Python 3

$ virtualenv venv3 --python /usr/local/bin/python3

Arbeiten mit einer virtuellen Umgebung