代码之家  ›  专栏  ›  技术社区  ›  bbartling

Jupyter实验室或pandas可以提供静态文件吗?

  •  0
  • bbartling  · 技术社区  · 2 年前

    如果我在经营一家 Jupyter notebook server 分析员可以浏览服务器URL来运行IPython(典型的IPython,但托管在远程),分析员可以将CSV格式的数据导出到本地机器吗?比如从熊猫数据争吵过程中下载他们正在处理的CSV文件?

    # 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();
    

    Jupyter实验室会像web服务器一样提供静态文件吗?例如,Flask服务器可以提供静态文件,很抱歉不是web开发人员,但我已经在Flask中进行了实验。烧瓶代码如下所示 send_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)
    

    如果IPython托管在远程计算机上,IPython或pandas能否提供静态文件?

    0 回复  |  直到 2 年前
    推荐文章