0% found this document useful (0 votes)
24 views5 pages

Pyscript SQLite Integration Guide

The document discusses using SQLite with Pyscript in Python. It imports Pandas and connects to a SQLite database. It reads data from a CSV file into a Pandas dataframe, inserts the data into a SQLite table, then reads from the table into another dataframe which it prints out. It provides two code examples, one to insert data and select a sample, another to group and aggregate data from the table.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Pyscript SQLite Integration Guide

The document discusses using SQLite with Pyscript in Python. It imports Pandas and connects to a SQLite database. It reads data from a CSV file into a Pandas dataframe, inserts the data into a SQLite table, then reads from the table into another dataframe which it prints out. It provides two code examples, one to insert data and select a sample, another to group and aggregate data from the table.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

14/07/2022 10:46 Pyscript: usando sqlite com Pyscript | programação python

Pyscript: usando sqlite com Pyscript

importar pandas

aqui vamos colocar a saída, no div chamado csv

Vamos importar essas coisas

[Link] 1/5
14/07/2022 10:46 Pyscript: usando sqlite com Pyscript | programação python

vamos pegar alguns dados

criamos o banco de dados e a conexão com o db (e criamos uma tabela?)

obtemos um dataframe da tabela de diamantes

vamos imprimir o dataframe

Código 1

1 <html>
2  
3 <head>

[Link] 2/5
14/07/2022 10:46 Pyscript: usando sqlite com Pyscript | programação python
4  
5 <title>
6  
7 Read CSV with Pandas using PyScript
8     
9 </title>
10  
11 <link rel="stylesheet" href="[Link] />
12 <script defer src="[Link]
13  
14 <!---pyodide-->
15  
16 <py-env>
17     - pandas
18 </py-env>
19  
20 </head>
21  
22 <body>
23     
24     <h1>Read CSV with Pandas using PyScript</h1>
25  
26     <p id="csv"></p>
27  
28     <py-script>
29  
30         import pandas as pd
31  
32         import sqlite3 as sql
33  
34  
35         from [Link] import open_url
36  
37         # read csv using pandas
38  
39         url_content = open_url("[Link]
er/[Link]")
40  
41         diamonds = pd.read_csv(url_content)
42  
43         # create a sqllite table
44  
45         conn = [Link]('[Link]')
46         diamonds.to_sql('diamonds', conn)
47  
48         # read from sql into a pandas dataframe
49  
50         df = pd.read_sql('SELECT * FROM diamonds LIMIT 7', conn)
51  
52         # print the output back
53         
54         csv = Element('csv')
55  
56         [Link](df)
57  
58         print([Link])
59  
60     </py-script>
61  
62 </body>
63  
64 </html>

[Link] 3/5
14/07/2022 10:46 Pyscript: usando sqlite com Pyscript | programação python

Código 2

1 <html>
2  
3 <head>
4  
5 <title>
6  
7 Read CSV with Pandas using PyScript
8     
9 </title>
10  
11 <link rel="stylesheet" href="[Link] />
12 <script defer src="[Link]
13  
14 <!---pyodide-->
15  
16 <py-env>
17     - pandas
18 </py-env>
19  
20 </head>
21  
22 <body>
23     
24     <h1>Read CSV with Pandas using PyScript</h1>
25  
26     <p id="csv"></p>
27  
28     <py-script>
29  
30         import pandas as pd
31  
32         import sqlite3 as sql
33  
34  
35         from [Link] import open_url
36  
37         # read csv using pandas
38  
39         url_content = open_url("[Link]
er/[Link]")
40  
41         diamonds = pd.read_csv(url_content)
42  
43         # create a sqllite table

[Link] 4/5
14/07/2022 10:46 Pyscript: usando sqlite com Pyscript | programação python
44  
45         conn = [Link]('[Link]')
46         diamonds.to_sql('diamonds', conn)
47  
48         # read from sql into a pandas dataframe
49  
50         df = pd.read_sql('SELECT cut, avg(price) as avg_price FROM diamonds group by 1', co
51 nn)
52  
53         # print the output back
54         
55         csv = Element('csv')
56  
57         [Link](df)
58  
59         print([Link])
60  
61     </py-script>
62  
63 </body>
64  
</html>

Assine a newsletter para atualizações


Post escrito por   Um entusiasta de python

[Link] 5/5

You might also like