6.8. Sharing and Downloading#
This section covers some tools to share and download your data.
6.8.1. Datapane: Publish your Python Objects on the Web in 2 Lines of Code#
Show code cell content Hide code cell content
!pip install datapane plotly
If you want to put together your pandas.DataFrame, interactive charts such as Plotly, Bokeh, Altair, or markdown into a nice report and publish it on the web, try Datapane. The code below shows how you can publish your Python objects using Datapane in a few lines of code.
import datapane as dp
import pandas as pd
import numpy as np
import plotly.express as px
# Scripts to create df and chart
df = px.data.gapminder()
chart = px.scatter(
df.query("year==2007"),
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=True,
size_max=60,
)
# Once you have the df and the chart, simply use
r = dp.Report(
dp.Text("my simple report"), # add description
dp.DataTable(df), # create a table
dp.Plot(chart), # create a chart
)
# Publish your report
r.upload(name="example")
Uploading report and associated data - please wait…
Report successfully uploaded, click here to view and share your report
6.8.2. gdown: Download a File from Google Drive in Python#
Show code cell content Hide code cell content
!pip install gdown
If you want to download a file from Google Drive in Python, use gdown. All you need to specify is the URL link.
import gdown
# Format of url: https://drive.google.com/uc?id=YOURFILEID
url = "https://drive.google.com/uc?id=1jI1cmxqnwsmC-vbl8dNY6b4aNBtBbKy3"
output = "Twitter.zip"
gdown.download(url, output, quiet=False)
Show code cell output Hide code cell output
Downloading...
From: https://drive.google.com/uc?id=1jI1cmxqnwsmC-vbl8dNY6b4aNBtBbKy3
To: /home/khuyen/book/book/Chapter4/Twitter.zip
120MB [00:09, 12.1MB/s]
'Twitter.zip'
6.8.3. pyserde: Effortless Serialization and Deserialization of Dataclass Objects#
Show code cell content Hide code cell content
!pip install pyserde
Dataclasses provide a concise syntax for defining data-holding classes. Paired with pyserde, you can easily serialize and deserialize dataclass objects.
Serialization converts dataclass data into a serialized format (e.g., JSON, YAML) for easy storage and transmission. Deserialization reconstructs the dataclass object from serialized data.
from dataclasses import dataclass
from serde import serde
from serde.json import from_json, to_json
from serde.yaml import from_yaml, to_yaml
@serde
@dataclass
class User:
name: str
age: int
user1 = User(name="user1", age=20)
print(to_json(user1))
{"name":"user1","age":20}
from_json(User, '{"name":"user1","age":20}')
User(name='user1', age=20)
print(to_yaml(user1))
age: 20
name: user1
from_yaml(User, "age: 20\nname: user1\n")
User(name='user1', age=20)
6.8.4. ItsDangerous: Safely Pass Trusted Data to Untrusted Environments and Back#
Show code cell content Hide code cell content
!pip install -U itsdangerous
When passing data between different web requests, there is a risk of malicious code injection.
To ensure the safety of passing data to untrusted environments, use ItsDangerous. ItsDangerous adds a unique signature to the data to verify that the data has not been tampered with during transmission.
from itsdangerous import URLSafeSerializer
auth_s = URLSafeSerializer("some key")
token = auth_s.dumps({"id": 5, "name": "khuyentran"})
token
'eyJpZCI6NSwibmFtZSI6ImtodXllbnRyYW4ifQ.3cQlkHP1MyeUS8jNQmGV_mbrxsQ'
# Get the data back from the token using the secret key
data = auth_s.loads(token)
data["name"]
'khuyentran'