1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Scheduling jupyter notebboks using Azure

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I have a notebook which reads csv files from sharepoint and uploads the files to snowflake. The code works fine in my local system. I want to schedule this notebook to run daily in the cloud. I explored Azure functions, created a function in vscode studio and also tried using Azure ML studio. Which is the best and cost effective solution for this. Here's the code I want to schedule.

    Can Azure runbooks be used too?

    from office365.runtime.auth.authentication_context import AuthenticationContext
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.files.file import File
    from office365.sharepoint.files.creation_information import FileCreationInformation
    # Import necessary modules
    import pandas as pd
    from io import StringIO
    from io import BytesIO
    import io
    import snowflake
    import sqlalchemy
    import snowflake.connector
    from snowflake.sqlalchemy import URL
    from sqlalchemy import create_engine,text
    from sqlalchemy.types import String

    from office365.runtime.auth.authentication_context import AuthenticationContext
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.files.file import File
    from office365.sharepoint.files.creation_information import FileCreationInformation
    # Import necessary modules
    import pandas as pd
    from io import StringIO
    from io import BytesIO
    import io
    import snowflake
    import sqlalchemy
    import snowflake.connector
    from snowflake.sqlalchemy import URL
    from sqlalchemy import create_engine,text
    from sqlalchemy.types import String

    ctx2 = connect_to_sharepoint(username, password, site_address)

    global print_folder_contents
    def print_folder_contents(ctx, folder_url):
    try:

    folder = ctx.web.get_folder_by_server_relative_url(folder_url)
    fold_names = []
    sub_folders = folder.files #Replace files with folders for getting list of folders
    ctx.load(sub_folders)
    ctx.execute_query()

    for s_folder in sub_folders:

    fold_names.append(s_folder.properties["Name"])

    return fold_names

    except Exception as e:
    print('Problem printing out library contents: ', e)

    # Function to read a CSV file from SharePoint
    def read_csv_from_sharepoint(ctx, file_url):
    try:
    file = ctx.web.get_file_by_server_relative_url(file_url)
    with BytesIO() as file_stream:
    file.download(file_stream)
    ctx.execute_query()
    file_stream.seek(0)
    return pd.read_csv(file_stream, dtype=object) # Ensure all columns are read as objects (strings)
    except Exception as e:
    print(f"Failed to read the file at {file_url}. Error: {e}")
    return None

    engine = create_engine(
    URL(
    user,
    account,
    authenticator='externalbrowser',
    database,
    schema,
    warehouse,
    ),
    connect_args={'connect_timeout': 120 }
    )


    try:
    connection = engine.connect()
    with engine.connect() as conn:
    results = conn.execute(text('select current_version()')).fetchone()
    print(results[0])
    df.columns = map(str.upper, df.columns)
    # Convert DataFrame columns to VARCHAR
    df = df.astype(str)
    df.to_sql('scheduler_test'.lower(), con=connection, schema=schema, index=False, if_exists='replace', chunksize=16000)

    finally:
    connection.close()
    engine.dispose()

    Continue reading...

Compartilhe esta Página