'Can Jupyter labs or pandas serve static files?

If I am running a Jupyter notebook server where an analyst can browse into server URL to run IPython, (typical IPython but hosted remotely) can the analyst export the data in CSV form as well to their local machine? Like download CSV file they are working on from a Pandas data wrangling process?

For example if a Pandas data frame is created something like this method below of IPython hosted remotely from an SQL query:

# Example python program to read data from a PostgreSQL table
# and load into a pandas DataFrame
import psycopg2
import pandas as pds
from sqlalchemy import create_engine

# Create an engine instance
alchemyEngine = create_engine('postgresql+psycopg2://test:@127.0.0.1', pool_recycle=3600);

# Connect to PostgreSQL server
dbConnection = alchemyEngine.connect();

# Read data from PostgreSQL database table and load into a DataFrame instance
dataFrame = pds.read_sql("select * from \"StudentScores\"", dbConnection);

pds.set_option('display.expand_frame_repr', False);

# Print the DataFrame
print(dataFrame);

# Close the database connection
dbConnection.close();

Would Jupyter labs have something to similar of serving a static file like a web server can do? For example a Flask server you can serve static file, sorry NOT a web developer but I have experimented with this in with Flask. The Flask code looks like this below for send_file to serve a static file:

from flask import Flask, request, send_file
import io
import os
import csv 
app = Flask(__name__)
@app.route('/get_csv')
def get_csv():
    """ 
    Returns the monthly weather csv file (Montreal, year=2019)
    corresponding to the month passed as parameter.
    """
    # Checking that the month parameter has been supplied
    if not "month" in request.args:
        return "ERROR: value for 'month' is missing"
    # Also make sure that the value provided is numeric
    try:
        month = int(request.args["month"])
    except:
        return "ERROR: value for 'month' should be between 1 and 12"
    csv_dir  = "./static"
    csv_file = "2019_%02d_weather.csv" % month
    csv_path = os.path.join(csv_dir, csv_file)
    
    # Also make sure the requested csv file does exist
    if not os.path.isfile(csv_path):
        return "ERROR: file %s was not found on the server" % csv_file
    # Send the file back to the client
    return send_file(csv_path, as_attachment=True, attachment_filename=csv_file)

Can a IPython or pandas serve a static file if IPython is hosted on a remote machine?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source