Jupyter-to-Tex/JupyterConnector.ipynb

8.3 KiB

In [1]:
import warnings

warnings.simplefilter(action="ignore", category=FutureWarning)

Start here

Install couchbase and pandas with specified versions

The cell below installs the couchbase python package and pandas.

In [2]:
!pip install couchbase==4.1.1 -q
!pip install pandas==1.5.2 -q

Import the the classes necessary to run the couchbase connector.

In [3]:
import pandas as pd
from couchbase.cluster import Cluster
from couchbase.auth import PasswordAuthenticator

from couchbase.options import ClusterOptions, QueryOptions

Connect to the cluster select the sample bucket and connect to the default collection.

In [4]:
cluster = Cluster.connect(
    "couchbase://couchbase1",
    ClusterOptions(
        PasswordAuthenticator("Administrator", "some-pw-that-is-better-than-this!")
    ),
)
bucket = cluster.bucket("travel-sample")

# get a reference to the default collection
cb_coll = bucket.default_collection()

Acces a document by key.

In [5]:
# get a document by key
result = cb_coll.get("airline_10")
result.value
Out[5]:
{'id': 10,
 'type': 'airline',
 'name': '40-Mile Air',
 'iata': 'Q5',
 'icao': 'MLA',
 'callsign': 'MILE-AIR',
 'country': 'United States'}

The Querry language contains a If a hyphen is used please use a pair of ` to surround the name using a hyphen.

In [6]:
result = cluster.query(
    """
    SELECT airport.* 
    FROM `travel-sample`.inventory.airport airport
    """,
    QueryOptions(metrics=True),
)
df = pd.DataFrame(result)
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1968 entries, 0 to 1967
Data columns (total 9 columns):
 #   Column       Non-Null Count  Dtype 
---  ------       --------------  ----- 
 0   airportname  1968 non-null   object
 1   city         1968 non-null   object
 2   country      1968 non-null   object
 3   faa          1709 non-null   object
 4   geo          1968 non-null   object
 5   icao         1687 non-null   object
 6   id           1968 non-null   int64 
 7   type         1968 non-null   object
 8   tz           1968 non-null   object
dtypes: int64(1), object(8)
memory usage: 138.5+ KB
In [11]:
result = cluster.query(
    """
    SELECT airport.airportname, airport.city, airport.country 
    FROM `travel-sample`.inventory.airport airport INNER JOIN `travel-sample`.inventory.landmark landmark on airport.city = landmark.city
    """,
    QueryOptions(metrics=True),
)
print(pd.DataFrame(result).sample(5).to_latex(bold_rows=True))
\begin{tabular}{llll}
\toprule
{} &           airportname &    city &         country \\
\midrule
\textbf{4877} &       London Heliport &  London &  United Kingdom \\
\textbf{4289} &     London St Pancras &  London &  United Kingdom \\
\textbf{3411} &              Stansted &  London &  United Kingdom \\
\textbf{6457} &  London - Kings Cross &  London &  United Kingdom \\
\textbf{8759} &     Charles De Gaulle &   Paris &          France \\
\bottomrule
\end{tabular}

In [13]:
result = cluster.query(
    """
    SELECT airport.airportname, airport.city, airport.country, landmark.name
    FROM `travel-sample`.inventory.airport airport INNER JOIN `travel-sample`.inventory.landmark landmark on airport.city = landmark.city
    """,
    QueryOptions(metrics=True),
)
print(pd.DataFrame(result).sample(5).sort_index().to_latex(bold_rows=True))
\begin{tabular}{lllll}
\toprule
{} &                        airportname &       city &         country &                     name \\
\midrule
\textbf{443 } &                          Edinburgh &  Edinburgh &  United Kingdom &     Murrayfield Ice Rink \\
\textbf{1487} &                              Luton &     London &  United Kingdom &              The Phoenix \\
\textbf{3620} &                 Paddington Station &     London &  United Kingdom &    Kensal Green Cemetery \\
\textbf{5629} &  London-Corbin Airport-MaGee Field &     London &   United States &         Alexandra Palace \\
\textbf{8588} &                         Le Bourget &      Paris &          France &  Aux Merveilleux de Fred \\
\bottomrule
\end{tabular}

End here

This should not be shownm