▶ 자연과학/▷ Python

【Python】 파이썬 앱 라이브러리

초록E 2023. 11. 15. 11:35

파이썬 앱 라이브러리(Python app library)

 

추천글 : 【컴퓨터과학】 컴퓨터과학 목차, 【Python】 파이썬 목차 


1. 정사면체의 3차원 뷰어 [본문]

2. 파이썬 시각화를 위한 Bokeh 설치하기 [본문]

3. 파이썬으로 html 형식의 글을 자동 생성하는 것 (automatic PDF report) [본문]

4. 메타 Segment-Anything Model (SAM) + 공간전사체 분석 (IAMSAM) [본문]

5. 유기화합물 3차원 분자식 [본문]


📃 파이썬 프로젝트 만들기: app.py 

파이썬 코드를 이용한 앱 제작 과정 

🖥️ html 앱 라이브러리 


 

1. 정사면체의 3차원 뷰어 [목차]

 

import dash
from dash import html, dcc
from dash.dependencies import Input, Output
import plotly.graph_objs as go

# Create a Dash app
app = dash.Dash(__name__)

# Define the vertices of the tetrahedron and their labels
vertices = [
    [0, 0, 1], [1, 0, 0], [0, 1, 0], [1, 1, 1],  # Tetrahedron vertices
    [0.5, 0.5, 0.5]  # Center point
]
labels = ['A', 'B', 'C', 'D', 'Center']  # Corresponding labels

# Define the edges of the tetrahedron
edges = [
    [0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]
]

# Create a list for edge traces
edge_traces = []
for edge in edges:
    x0, y0, z0 = vertices[edge[0]]
    x1, y1, z1 = vertices[edge[1]]
    edge_traces.append(
        go.Scatter3d(
            x=[x0, x1, None],  # the None here is important to create discontinuity between the edges
            y=[y0, y1, None],
            z=[z0, z1, None],
            mode='lines',
            line=dict(color='red', width=5)
        )
    )

# Create a trace for vertices with labels
vertex_trace = go.Scatter3d(
    x=[v[0] for v in vertices],
    y=[v[1] for v in vertices],
    z=[v[2] for v in vertices],
    mode='markers+text',
    marker=dict(color='black', size=5),
    text=labels,
    textposition='top center'
)

# Define the layout of the plot without the drawing tool configuration
layout = go.Layout(
    scene=dict(
        xaxis=dict(title='X axis'),
        yaxis=dict(title='Y axis'),
        zaxis=dict(title='Z axis'),
    ),
    margin=dict(l=0, r=0, b=0, t=0),  # margins to zero for full usage of space
    uirevision='constant',  # Preserve UI state between updates
)

# Create the figure and add the traces
fig = go.Figure(data=edge_traces + [vertex_trace], layout=layout)

# Define the app layout
app.layout = html.Div([
    dcc.Graph(
        id='3d-plot',
        figure=fig,
        config={
            'modeBarButtonsToAdd': [
                'drawline',
                'drawopenpath',
                'drawclosedpath',
                'drawcircle',
                'drawrect',
                'eraseshape'
            ]
        }
    )
])

# Run the server
if __name__ == '__main__':
    app.run_server(port=8927, host='0.0.0.0', debug=True)

 

(▼ 모바일 버전에서 작동이 원활합니다.)

 

 

 

2. 파이썬 시각화를 위한 Bokeh 설치하기 [목차]

 

 

3. 파이썬으로 html 형식의 글을 자동 생성하는 것 (automatic PDF report) [목차]

 

 

4. 메타 Segment-Anything Model (SAM) + 공간전사체 분석 (IAMSAM) [목차]

깃허브 repo 

앱 링크 

 

 

5. 유기화합물 3차원 분자식 [목차]

 

from rdkit import Chem
from rdkit.Chem import AllChem
import py3Dmol

def draw_3d_molecule(smiles):
    # Convert SMILES to RDKit molecule
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        print("Invalid SMILES code.")
        return
    
    # Generate 3D coordinates for the molecule
    mol = Chem.AddHs(mol)  # Add hydrogens
    AllChem.EmbedMolecule(mol, AllChem.ETKDG())  # Embed molecule in 3D space
    
    # Convert RDKit molecule to 3Dmol.js viewable format
    mb = Chem.MolToMolBlock(mol)
    
    # Visualization with Py3Dmol
    viewer = py3Dmol.view(width=400, height=300)
    viewer.addModel(mb, 'mol')
    viewer.setStyle({'stick': {}})
    viewer.zoomTo()
    
    return viewer.show()

# Example usage
smiles_code = "CCO"  # Ethanol
draw_3d_molecule(smiles_code)

 

 

입력: 2023.11.15 11:20

수정: 2024.02.19 23:41